Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hi I am adding options to WP Customize.

Mostly everything works, but One of my variable is not refreshed in the preview window.

$wp_customize->add_setting('checkbox', array(
        'capability' => 'edit_theme_options',
        'default' => false,
    ));

$wp_customize->add_control('checkbox_control', array(
    'settings' => 'checkbox',
    'label'    => __('Activate Coming Soon Page', 'ln'),
    'section'  => 'general_settings',
    'type'     => 'checkbox',
    'priority' => 1,
));

This is how I add the checkbox and here is my check in functions.php

function template_redirect() {
 if(true == get_theme_mod('checkbox')) {
        include (TEMPLATEPATH . '/coming-soon.php');
        exit;
    }
}
add_action( 'template_redirect', 'template_redirect' );

So everything works. ~The only problem is that the preview window when you are in WP Customize and click the checkbox it reloads the page but it doesn't shows the change. If I click save and refresh manually then everything is fine.

Any suggestion how to fix this issue?

Thank you

UPDATE:

I found what is breaking WP CUSTOMIZER, it is the exit of the redirect. Is there another way how to fix this issue. I need to redirect to that page if the checkbox is TRUE

 function template_redirect() {
     if(true == get_theme_mod('checkbox')) {
            include (TEMPLATEPATH . '/coming-soon.php');
            exit;  !!!!!!!!!!!!!!!!!
        }
    }
    add_action( 'template_redirect', 'template_redirect' );
share|improve this question

1 Answer 1

Have you tried explicitly adding the 'transport' option when you call $wp_customize->add_setting?

e.g.

$wp_customize->add_control('checkbox_control', array(
    'settings' => 'checkbox',
    'label'    => __('Activate Coming Soon Page', 'ln'),
    'section'  => 'general_settings',
    'type'     => 'checkbox',
    'priority' => 1,
    'transport'   => 'refresh'
));

Alternatively, you could use a Framework to interact with the WP Theme Customizer. For example, there's a good one by Slobodan Manic call Customizer Boilerplate. For example, to use a checkbox such as yours in Customizer Boilerplate, you would simple download the files to your theme's directory from Github, include a call in your themes functions.php to Customizer Boilerplate (e.g. require( get_stylesheet_directory() . '/customizer-boilerplate/customizer.php' ); then you could modify the options provided in options.php to leave just the checkbox:

                /*
             * ==============
             * ==============
             * Checkbox field
             * ==============
             * ==============
             */
            'new_checkbox_field' => array(
                'setting_args' => array(
                    'default' => true,
                    'type' => 'option',
                    'capability' => $thsp_cbp_capability,
                    'transport' => 'refresh',
                ),                  
                'control_args' => array(
                    'label' => __( 'New checkbox field label', 'my_theme_textdomain' ),
                    'type' => 'checkbox', // Checkbox field control
                    'priority' => 2
                )
            )
share|improve this answer
    
Thank you for the reply. I tried adding 'transport' => 'refresh' but it didn't work. What I noticed is when you change the status of the checkbox the AJAX is returning the correct HTML and is changing according the option but the preview is not updated with the new response from the AJAX. I cannot understand why. –  user43506 Mar 17 at 6:51
    
What code do you have in your theme template to get the option from the checkbox? (Please either edit/comment your original question with updated information rather than adding a new answer) –  MartinB Mar 19 at 15:21
    
I am taking the variable with get_theme_mod() –  user43506 May 7 at 14:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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