1

Here is my code:

// Hooked to "after_setup_theme"
if(get_theme_mod('enable_vertical_header')) {
    // init vertical header, register scripts, style, settings and output html in template
    $vertical_header = new Vertical_Header;
}

I created a checkbox option "enable_vertical_header" (default value 0) in Customizer to turn on/off the vertical header, when I toggle the option, the live preview area refreshes, but nothing change.

By echo the value of "enable_vertical_header", I see it always null, which make sense if on frontend. But in Customizer, should get_theme_mod not return the real-time value?


Update: the Control code

function my_custom_text_settings( $wp_customize ) {
    $wp_customize->add_section( 'my_section' , array(
        'title'      => __('My Section','mytheme'),
        'priority'   => 30,
    ) );

    // Register the settings
    $wp_customize->add_setting( 'enable_vertical_header', array(
        'default'        => 0,
        'type'           => 'theme_mod',
        'capability'     => 'edit_theme_options',
    ) );
    // Add the controls
    $wp_customize->add_control( 'enable_vertical_header', array(
        'label'       => __( 'My custom control', 'translation_domain' ),
        'section'     => 'my_section',
        'settings'    => 'enable_vertical_header',
        'type'        => 'checkbox',
        'priority'    => 10
    ) );

}
add_action( 'customize_register', 'my_custom_text_settings' );
  • this code is just half of the story, what is the code for the control you use? – Mark Kaplun Jan 15 '17 at 3:35
  • @MarkKaplun I use Kirki, to prevent confusion, I translated it to plain WordPress code, just a simple checkbox, nothing special. – Edward Jan 15 '17 at 3:51
2

Well, by some experiments, I found the answer.

get_theme_mod won't get you the real-time value when called in 'after_setup_theme' or 'init', it does work in 'customize_preview_init' and 'wp_loaded', so the working code:

add_action('customize_preview_init', 'my_header' );
function my_header() {
        if(get_theme_mod('enable_vertical_header')) {
            $vertical_header = new Vertical_Header;
        }   
}
  • that is wrong. the problem is more likely that you are trying to access the data before the customizer had finished initializing things. Nothing should ever be hooked before the init (and better wp_loaded) hook without a very good reason, especially things that are related to front end which probably should not be done before template_redirect – Mark Kaplun Jan 15 '17 at 6:06
  • @MarkKaplun init not working either, wp_loaded does, but it is too late for some hooks, say 'widget_init', which is used to register widgetized area in my Vertical Header – Edward Jan 15 '17 at 7:04
  • Theme options and any other thing can be overridden by plugins or themes. The most safe place for front end is to do things after wordpress boot had finished and better after wordpress had decided it is a fron end request. Even widgets might be deregistered during boot time. I don't know what is that you are trying to do, but it sounds like it involves outputing HTML and CSS which means it should wait for wp_head. As a ,matter of principal in wordpress development you should always use the latest hook as possible – Mark Kaplun Jan 15 '17 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.