Checkbox
controls provide a simple true/false choice to the user.
If a checkbox is not what you’re after visually, you can also use a Switch
or a Toggle
. Technically speaking, they are both checkboxes as well, so you use them the exact same way. Their only difference is the way they look.
Example
new \Kirki\Field\Checkbox(
[
'settings' => 'checkbox_setting',
'label' => esc_html__( 'Checkbox Control', 'kirki' ),
'description' => esc_html__( 'Description', 'kirki' ),
'section' => 'section_id',
'default' => true,
]
);
Usage
<?php if ( true == get_theme_mod( 'checkbox_setting', true ) ) : ?>
<p>Checkbox is checked</p>
<?php else : ?>
<p>Checkbox is unchecked</p>
<?php endif; ?>
Adding a class to a div
if the checkbox is checked:
<?php $value = get_theme_mod( 'checkbox_setting', true ); ?>
<div class="<?php echo ( $value ) ? 'checkbox-on' : 'checkbox-off'; ?>">
If the checkbox is checked, the class will have a class "checkbox-on".
If the checkbox is unchecked, the class will have a class "checkbox-off".
</div>