Adding a checkbox to the theme customizer

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):

Read More
$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.

Related posts

Leave a Reply

2 comments

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

    At first, you must define the setting, via add_setting, important is the parameter type with value option. After this, control the field via add_controland set the parameter type to checkbox. Alternative, it is possible to use select. If I add a default value via std, 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 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.

  2. 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',
            )
        )
    );