简体   繁体   中英

Wordpress customizer preview doesn't refresh functions.php

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><a href="https://example.com">Custom Link</a></li>';
        }
    }
    return $items;
}
add_filter('wp_nav_menu_items', 'wpso_customize_menu', 10, 2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM