8

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.

4
  • hey, did you resolve this issue? Same here. It gets somehow checked via JS. The comment on ottopress with 'unique ID' didn't help for me.
    – xsonic
    Nov 12, 2012 at 14:08
  • 1
    My issue was resolved when I made sure the ID used in $wp_customize->add_control() was unique.
    – dgwyer
    Nov 13, 2012 at 14:30
  • @dgwyer, please, add your solution as an Answer, so this Question don't remains in the "Unanswered" list.
    – brasofilo
    Feb 20, 2013 at 18:58
  • how do I do that?
    – dgwyer
    Feb 21, 2013 at 20:15

2 Answers 2

4

Checkbox is possible. A example, I hope this help you.

At first you must define the setting, via add_setting, important is the param type with value option. After this control the field via add_controland set the param type to checkbox. Alternative it is possible to use select. If I add a default value via std, then work it, also without this param. Alternative works also fine, if I add the choices parameter with the values 1 and 0. But on the tests works fine, if I set the param only to checkbox. 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:

    case 'checkbox':
        var_dump( $this->value() );

Example:

    // Add settings for output description
    $wp_customize->add_setting( $this->option_key . '[echo_desc]', array(
        'default'    => $defaults['echo_desc'],
        'type'       => 'option',
        'capability' => 'edit_theme_options'
    ) );

    // Add control and output for select field
    $wp_customize->add_control( $this->option_key . '_echo_desc', array(
        'label'      => __( 'Display Description', 'documentation' ),
        'section'    => 'title_tagline',
        'settings'   => $this->option_key . '[echo_desc]',
        'type'       => 'checkbox',
        'std'        => '1'
    ) );

See the result of this source. screenshot depicting the checkbox in the WordPress customizer

You find a working result in my theme Documentation, hosted on Github.

3

I was having a similar issue, and it turned out that setting 'type' => 'option' for add_setting was the cause.

Removing this solved my problem, and below is what I am currently using and it works just fine.

$wp_customize->add_section('footer_social_media_section' , array(
    'title'     => __('Footer Social Media', 'dd_theme'),
    'priority'  => 1020
));

$wp_customize->add_setting('show_footer_facebook', array(
    'default'    => '1'
));

$wp_customize->add_control(
    new WP_Customize_Control(
        $wp_customize,
        'show_footer_facebook',
        array(
            'label'     => __('Show Facebook Link', 'dd_theme'),
            'section'   => 'footer_social_media_section',
            'settings'  => 'show_footer_facebook',
            'type'      => 'checkbox',
        )
    )
);

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.