Switches provide a simple way to turn options on or off. They return a boolean
which makes it easy for developers to build logic around those types of controls.
Switch controls are making use of the Checkbox
control. They are simply styled differently.
One main difference between the Switch
control and the Checkbox
or Toggle
control is that switches can change labels.
By default, the labels are On and Off. To change them you can use the choices
argument:
'choices' => [
'on' => esc_html__( 'Enable', 'kirki' ),
'off' => esc_html__( 'Disable', 'kirki' ),
]
Example
new \Kirki\Field\Checkbox_Switch(
[
'settings' => 'switch_setting',
'label' => esc_html__( 'Switch Field', 'kirki' ),
'description' => esc_html__( 'Simple switch control', 'kirki' ),
'section' => 'section_id',
'default' => 'on',
'choices' => [
'on' => esc_html__( 'Enable', 'kirki' ),
'off' => esc_html__( 'Disable', 'kirki' ),
],
]
);
Usage
<?php if ( true == get_theme_mod( 'switch_setting', 'on' ) ) : ?>
<p>Switch is ON</p>
<?php else : ?>
<p>Switch is OFF</p>
<?php endif; ?>