switch

Switches provide a simple way to turn on/off options. They return a boolean so you can easily check their value in your code and act on them.

Switch controls are internally checkbox controls styled differently.

One main difference that switch controls have from checkbox and toggle controls is that on switches you can change their labels.

By default the labels are ON/OFF. To change them you can use the choices argument:

'choices' => [
    'on'  => esc_html__( 'Enable', 'kirki' ),
    'off' => esc_html__( 'Disable', 'kirki' )
]

Example

Switches have the benefit of allowing you to change their labels. In the example below we’ll be using ‘Enable’ and ‘Disable’ as labels. The default labels are “On” & “Off”, so if you don’t want to change them you can simply omit the choices argument.

Kirki::add_field( 'theme_config_id', [
	'type'        => 'switch',
	'settings'    => 'switch_setting',
	'label'       => esc_html__( 'This is the label', 'kirki' ),
	'section'     => 'section_id',
	'default'     => '1',
	'priority'    => 10,
	'choices'     => [
		'on'  => esc_html__( 'Enable', 'kirki' ),
		'off' => esc_html__( 'Disable', 'kirki' ),
	],
] );

Usage

<?php if ( true == get_theme_mod( 'switch_setting', true ) ) : ?>
	<p>Switch is ON</p>
<?php else : ?>
	<p>Switch is OFF</p>
<?php endif; ?>

Last updated on: May 10th, 2020

Scroll to Top

Download

Subscribe & be the first to be informed about
new features & updates!