When trying to add a checkbox to the theme customizer it seems to be ‘always’ selected. If you try to deselect it you cannot, almost as if there is some JS code forcing it to stay selected.
I am using serialized theme options and everything is hooked up correctly. Code is similar to the following (triggered via the ‘customize_register’ hook):
$wp_customize->add_setting( mytheme_options[chk_hide_description], array(
'default' => false,
'type' => 'option',
'capability' => 'edit_theme_options' )
);
$wp_customize->add_control( 'display_header_text', array(
'settings' => mytheme_options[chk_hide_description],
'label' => __( 'Hide site description' ),
'section' => 'title_tagline',
'type' => 'checkbox',
) );
Same issue reported here: http://ottopress.com/2012/how-to-leverage-the-theme-customizer-in-your-own-themes/#div-comment-11254.
Checkbox is possible. A example, I hope this help you.
At first, you must define the setting, via
add_setting
, important is the parametertype
with valueoption
. After this, control the field viaadd_control
and set the parametertype
tocheckbox
. Alternative, it is possible to useselect
. If I add a default value viastd
, then work it, also without this parameter. Alternative works also fine, if I add the choice’s parameter with the values 1 and 0. But on the tests, works fine, if I set the param only tocheckbox
. You find the source inside my project, see link below.Also, you can debug the output on value string on the line 246 in the wp-includes/class-wp-customize-control.php; maybe it helps.
debug:
Example:
See the result of this source.
You find a working result in my theme Documentation, hosted on GitHub.
I was having a similar issue, and it turned out that setting
'type' => 'option'
foradd_setting
was the cause.Removing this solved my problem, and below is what I am currently using and it works just fine.