So I ran into a issue where the customizer preview doesn't fully refresh. Only when I manually refresh the page I see my changes. Some of my code to help explain below.
For my customizer settings I have code that looks something like this
$wp_customize->add_section( 'theme_layout', array(
'title' => __( 'Layout', 'theme' ),
'priority' => 30
) );
$wp_customize->add_setting( 'theme_header_layout', array(
'default' => 'default',
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Control( $wp_customize,
'theme_header_layout', array(
'label' => __( 'Header', 'theme' ),
'section' => 'theme_layout',
'settings' => 'theme_header_layout',
'type' => 'select',
'choices' => array(
'default' => 'default',
'special_header' => 'Special Header',
)
) ) );
Now In my functions.php I have code like this
//this is the code that doesn't seem to execute when the customizer refreshes
if ( 'special_header' == get_theme_mod( 'theme_header_display' ) ):
function theme_special_header( $items ) {
$items .= do_shortcode('[special_header_shortcode]');//This shortcode exists I just didnt bother mentioning it here
}
add_action( 'wp_nav_menu_secondary-menu_items', 'theme_special_header' );//Adds shortcode to menu with id of secondary-menu
endif;
This all works great accept when I go to the customizer and select 'Special Header' the customizer refreshes and I don't see my changes until I completely refresh the page.
I had also faced similar issue earlier. Rather than adding conditional outside, I kept it inside the function and it worked. You can try similar approach for your code and it may help.
Following is not exact answer for your question but it may help to fix your problem.
function wpso_customize_menu($items, $args) {
if ( 'special_header' == get_theme_mod( 'theme_header_layout' ) ) {
if( $args->theme_location == 'menu-1' ) {
$items .= '<li>Custom Link</li>';
}
}
return $items;
}
add_filter('wp_nav_menu_items', 'wpso_customize_menu', 10, 2);
Related
I am working with ACF Pro and ACF Theme Code Pro plugins.
It correctly working within admin-panel but not working on site.
Maybe anyone know what i doing incorrectly?
my functions.php
add_action( 'acf/init', 'register_about_service_block' );
function register_about_service_block() {
if ( function_exists( 'acf_register_block_type' ) ) {
// Register About service block
acf_register_block_type( array(
'name' => 'about-service',
'title' => __( 'About service' ),
'description' => __( 'A custom About service block.' ),
'category' => 'formatting',
'icon' => 'layout',
'keywords' => array( 'about', 'service' ),
'post_types' => array( 'post', 'page' ),
'mode' => 'auto',
// 'align' => 'wide',
'render_template' => 'template-parts/blocks/about-service.php',
// 'render_callback' => 'about_service_block_render_callback',
'enqueue_style' => get_template_directory_uri() . '/dist/css/services.min.css',
// 'enqueue_script' => get_template_directory_uri() . '/template-parts/blocks/about-service/about-service.js',
// 'enqueue_assets' => 'about_service_block_enqueue_assets',
));
}
}
Plugin genereted code that i can use in my template. I use it in template and it correctly working in admin-panel. It looks like this
But on my page, where i use this template it not working. Еhe template itself is connected correctly.
function acf_register_block_type must return $block
function acf_register_block_type( $block ) {
// Validate block type settings.
$block = acf_validate_block_type( $block );
// Bail early if already exists.
if( acf_has_block_type($block['name']) ) {
return false;
}
// Add to storage.
acf_get_store( 'block-types' )->set( $block['name'], $block );
// Register block type in WP.
if( function_exists('register_block_type') ) {
register_block_type($block['name'], array(
'attributes' => acf_get_block_type_default_attributes(),
'render_callback' => 'acf_rendered_block',
));
}
// Register action.
add_action( 'enqueue_block_editor_assets', 'acf_enqueue_block_assets' );
// Return block.
return $block;
}
Then $block must be use in my template but when i make var_dump($block) i get null. Hence i can't use data by $block in template
I am looking to make some modifications to a function in WooCommerce, on a file called class-wc-frontend-scripts.php in woocommerce/includes/
The function I'm looking to modify is:
private static function get_script_data( $handle ) {
global $wp;
switch ( $handle ) {
case 'wc-single-product' :
return array(
'i18n_required_rating_text' => esc_attr__( 'Please select a rating', 'woocommerce' ),
'review_rating_required' => get_option( 'woocommerce_review_rating_required' ),
'flexslider' => apply_filters( 'woocommerce_single_product_carousel_options', array(
'rtl' => is_rtl(),
'animation' => 'slide',
'smoothHeight' => true,
'directionNav' => false,
'controlNav' => 'thumbnails',
'slideshow' => false,
'animationSpeed' => 500,
'animationLoop' => false, // Breaks photoswipe pagination if true.
) ),
'zoom_enabled' => apply_filters( 'woocommerce_single_product_zoom_enabled', get_theme_support( 'wc-product-gallery-zoom' ) ),
'photoswipe_enabled' => apply_filters( 'woocommerce_single_product_photoswipe_enabled', get_theme_support( 'wc-product-gallery-lightbox' ) ),
'photoswipe_options' => apply_filters( 'woocommerce_single_product_photoswipe_options', array(
'shareEl' => false,
'closeOnScroll' => false,
'history' => false,
'hideAnimationDuration' => 0,
'showAnimationDuration' => 0
) ),
'flexslider_enabled' => apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) ),
);
break;
}
return false;
}
For my product slider, I need to show prev and next arrows on single product page. So I need to change 'directionNav' as true.
How do I do this without making changes to the core files?
Have you created a child theme yet? If not, that's your first step. Here's a link from the WordPress codex:
https://codex.wordpress.org/Child_Themes
Then instead of editing that core file, you hook into that function instead, and write that code inside of the functions.php file you created in your child theme. If you want to add code to the existing function, you'll want to use an action hook. If you want to modify the code, you use a filter. It looks like you're trying to change the code, so probably a filter is best.
Here's how it would look using a filter:
add_filter( 'get_script_data', 'change_nav_direction' );
function change_nav_direction( $variable ) {
//any other code you may need
'directionNav' => true,
//you must have a return
return $variable;
}
Here's a link to an article from WooCommerce that might help:
https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/
I'm being requested by my client to add a custom field that they will be able to enter in a url. The post itself is a custom plugin custom post type, this is the code I have for this portion:
register_post_type( 'storylist',
array(
'labels' => $labels,
'public' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'show_ui' => true,
'supports' => array('title'),
)
);
add_filter( 'rwmb_meta_boxes', 'c_register_meta_boxes' );
}
function c_register_meta_boxes( $boxes ){
$prefix = 'c_rwmb_';
$boxes[] = array(
'id' => 'view',
'title' => __('View Link', 'c_rwmb' ),
'post_types' => array('storylist'),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => __('View URL', 'c_rwmb' ),
'id' => $prefix . 'view_url',
'type' => 'text',
'size' => 60,
'clone' => false
),
)
);
return $meta_boxes;
}
Now the problem is when I go to the post, I do not see the custom meta field even showing up, is there something that I'm missing?
The custom post type("storylist") comes from the plugin right? Then you don't need to register the custom post again. You just needs to add meta field for this post type and save its value while updating the post. Once I had a experience to enable/disable sidebar using custom field. I shared my code. Hope this will help you.
<?php
add_action('admin_init','add_metabox_post_sidebar');
add_action('save_post','save_metabox_post_sidebar');
/*
* Funtion to add a meta box to enable/disable the posts.
*/
function add_metabox_post_sidebar()
{
add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high");
}
function enable_sidebar_posts(){
global $post;
$check=get_post_custom($post->ID );
$checked_value = isset( $check['post_sidebar'] ) ? esc_attr( $check['post_sidebar'][0] ) : 'no';
?>
<label for="post_sidebar">Enable Sidebar:</label>
<input type="checkbox" name="post_sidebar" id="post_sidebar" <?php if($checked_value=="yes"){echo "checked=checked"; } ?> >
<p><em>( Check to enable sidebar. )</em></p>
<?php
}
/*
* Save the Enable/Disable sidebar meta box value
*/
function save_metabox_post_sidebar($post_id)
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
$checked_value = isset( $_POST['post_sidebar'] ) ? 'yes' : 'no';
update_post_meta( $post_id, 'post_sidebar', $checked_value );
}
?>
Here I have added a custom field called 'post_sidebar' for post type "post" you can change your own and change your post type in this line add_meta_box("Enable Sidebar", "Enable Sidebar", "enable_sidebar_posts", "post", "side", "high"); from "post" to "storylist".
Just for somebody who needs to add MetaBox fields via plugin, because the question is:
How to add custom fields to a WordPress Plugin
function gffgfg_add_boxes( $meta_boxes ) {
$prefix = 'some prefix';
$meta_boxes[] = array(
//metabox array
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'gffgfg_add_boxes', 999 ); // 999
I am trying to remove the ability to change the 'site icon' in a Wordpress site unless the user is a 'Super Admin'.
My first thought was trying to modify this code snippet here that is located in the **/wp-includes/class-wp-customize-manager.php
$this->add_setting( 'site_icon', array(
'type' => 'option',
'capability' => 'manage_options',
'transport' => 'postMessage', // Previewed with JS in the Customizer controls window.
) );
$this->add_control( new WP_Customize_Site_Icon_Control( $this, 'site_icon', array(
'label' => __( 'Site Icon' ),
'description' => sprintf(
/* translators: %s: site icon size in pixels */
__( 'The Site Icon is used as a browser and app icon for your site. Icons must be square, and at least %s pixels wide and tall.' ),
'<strong>512</strong>'
),
'section' => 'title_tagline',
'priority' => 60,
'height' => 512,
'width' => 512,
) ) );
But I do not want to change any core/delivered files. Is there any other way to accomplish this? Maybe in the theme's functions.php file?
Also, I'm currently using WordPress 4.5.2 and the twentytwelve theme.
function remove_styles_sections($wp_customize) {
$wp_customize->remove_control('site_icon');
}
add_action( 'customize_register', 'remove_styles_sections', 20, 1 );
This is what I did in order to do remove the Site Icon functionality.
if( !is_super_admin( get_current_user_id()) ){
function remove_styles_sections(){
global $wp_customize;
$wp_customize->remove_control('site_icon');
}
// Priority 20 so that we remove options only once they've been added
add_action( 'customize_register', 'remove_styles_sections', 20 );}
So I'm trying to build a conditional statement to display different layouts depending on the option selected.
Having a bit of trouble.
$wp_customize->add_setting('layout', array(
'default' => 'stream',
)); // add our default setting.
$wp_customize->add_control('layout', array(
'label' => __('Select layout', 'Ari'),
'section' => 'layout',
'settings' => 'layout',
'type' => 'radio',
'choices' => array(
'stream' => 'Stream',
'grid' => 'Grid',
),
)); // use radio buttons with (so far) two choices.
$wp_customize->add_section('layout' , array(
'title' => __('Layout','Ari'),
)); // add our layout section in the customize panel.
Displaying our layout.
<?php if ( get_theme_mod( 'stream' ) ) : ?>
<p>stream</p> if ( have_posts() ) etc
<?php elseif ( get_theme_mod( 'grid' ) ) : ?>
<p>grid</p> // if ( have_posts() ) etc
<?php else : //Nothing ?>
<?php endif; ?>
Just...nothing. I've trawled through the codex and I can't see what I'm doing wrong. It just doesn't show any output.
#Stewartside points out that that I should use get_setting, I can't get this working though. Anyone?
get_theme_mod() returns a string, not a boolean. Therefore, your if statements will always fail as they are expecting either 1 or true or 0 or false.
You should look into using get_setting within the $wp_customize functionality.
Documentation