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' );
A likely reason you are not able to see the change in the preview is that your
coming-soon.php
template doesn’t include the requisitewp_head()
anwp_footer()
calls being made. If you look at your browser’s DOM, you should actually see that a secondaryiframe
is getting created behind the visibleiframe
. However, if your template doesn’t include the standard theme hooks then thecustomize-preview.js
script won’t be enqueued and it won’t be able to communicate to the parent window that theiframe
has loaded and thus is ready to replace the previousiframe
.This same question was actually reported to WP Core Trac in #37981: “template_include not working in customizer previewer” in which I answered similarly:
Have you tried explicitly adding the
'transport'
option when you call$wp_customize->add_setting
?e.g.
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 inoptions.php
to leave just the checkbox:You should only use template_redirect when you don’t want anything else hooked to template_redirect to be executed. Instead, to include an alternative template, you must use
template_include
There are other filters, like
single_template
orarchive_template
. You can find more info at the Codexhttp://codex.wordpress.org/Plugin_API/Filter_Reference/template_include