Checkbox Field Type

Introduction

The checkbox type field, part of the Settings API, renders one or more checkbox type inputs.

Example

The following example shows a section with a checkbox type field. The default_value property is set to 1 for each choice, which means the box will be checked by default. The default_value will no longer apply once the user makes a selection and submits.

You may also include other HTML attributes as properties for fields. In this example, you will see the onclick property added. This means when the onclick event fires, a javascript alert will display the text “This is a test”.

array(
    'title' => 'Checkbox Field',
    'fields' => array(
        array(
            'type'    => 'checkbox',
            'name'    => 'mycheckbox',
            'label'   => esc_html__( 'This is the label for my checkbox field.', 'sometextdomain' ),
            'onclick' => 'alert("This is a test.")',
            'choices' => array(
                array(
                    'label'         => esc_html__( 'A label for choice1 option.', 'sometextdomain' ),
                    'name'          => 'choice1',
                    'tooltip'       => esc_html__( 'A tooltip for choice1 option.', 'sometextdomain' ),
                    'default_value' => 1,
                ),
                array(
                    'label'         => esc_html__( 'A label for choice2 option.', 'sometextdomain' ),
                    'name'          => 'choice2',
                    'tooltip'       => esc_html__( 'A tooltip for choice2 option.', 'sometextdomain' ),
                ),
            ),
        ),
    )
)

The code above will render the checkbox field similar to the following:

Uses