IntroductionExampleUses
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」.
1234567891011121314151617181920212223242526array( 'title' => esc_html__( 'This is the title for Section 3', 'sometextdomain' ), 'fields' => array( array( 'type' => 'checkbox', 'name' => 'mycheckbox', 'label' => esc_html__( 'This is the label for my checkbox', 'sometextdomain' ), 'onclick' => 'alert("This is a test.")', 'choices' => array( array( 'label' => esc_html__( 'this is the label for choice1', 'sometextdomain' ), 'name' => 'choice1', 'tooltip' => esc_html__( 'this is the tooltip for choice1', 'sometextdomain' ), 'default_value' => 1, ), array( 'label' => esc_html__( 'this is the label for choice2', 'sometextdomain' ), 'name' => 'choice2', 'tooltip' => esc_html__( 'this is the tooltip for choice2', 'sometextdomain' ), 'default_value' => 1, ), ), ), ),),
The code above will render the checkbox field similar to the following:
Uses
settings_checkbox()
checkbox_item()
checkbox_input()
checkbox_input_{$choice_name}()