Adding a checkbox to the theme customizer

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.

Leave a Comment