Skip to content
Controls

Checkbox & Toggle

Checkbox and toggle controls let you manage boolean (true/false) values or multiple choices in your blocks. Both controls provide similar functionality with different visual appearance.

Checkbox Control
Toggle Control

Control Settings

Single Checkbox/Toggle

  • Alongside Text - Text displayed next to the control
  • Checked - Set the default checked state

Multiple Checkboxes

When Multiple option is enabled, the following settings are available:

  • Alongside Text - Text displayed next to the control
  • Multiple - Enable multiple checkboxes
  • Choices - Add options for multiple checkboxes
  • Output Format - Specifies the returned value format. Choose from Value, Label or Both (Array)
Checkbox choices setting

Usage

Single Checkbox/Toggle

PHP
<?php if ( $attributes['control_name'] ) : ?>
  <p>The value is True</p>
<?php else: ?>
  <p>The value is False</p>
<?php endif; ?>
Handlebars
{{#if control_name}}
  <p>The value is True</p>
{{else}}
  <p>The value is False</p>
{{/if}}

Multiple Checkboxes

PHP with Value format
<?php
// Returns array of selected values
foreach ( $attributes['control_name'] as $value ) {
    echo '<p>' . esc_html( $value ) . '</p>';
}
?>
PHP with Label format
<?php
// Returns array of selected labels
foreach ( $attributes['control_name'] as $label ) {
    echo '<p>' . esc_html( $label ) . '</p>';
}
?>
PHP with Both format
<?php
// Returns array of arrays with value and label
foreach ( $attributes['control_name'] as $choice ) {
    echo '<p>Value: ' . esc_html( $choice['value'] ) . '</p>';
    echo '<p>Label: ' . esc_html( $choice['label'] ) . '</p>';
}
?>
Handlebars with Value format
{{#each control_name}}
  <p>{{this}}</p>
{{/each}}
Handlebars with Label format
{{#each control_name}}
  <p>{{this}}</p>
{{/each}}
Handlebars with Both format
{{#each control_name}}
  <p>Value: {{this.value}}</p>
  <p>Label: {{this.label}}</p>
{{/each}}

Post Meta

Single Checkbox
<?php if ( get_lzb_meta( 'control_meta_name' ) ) : ?>
  <p>The value is True</p>
<?php else: ?>
  <p>The value is False</p>
<?php endif; ?>
Multiple Checkboxes
<?php
$selected_choices = get_lzb_meta( 'control_meta_name' );
 
// Output depends on selected Output Format
foreach ( $selected_choices as $choice ) {
    echo '<p>' . esc_html( $choice ) . '</p>';
}
?>

When Multiple option is enabled, the control returns an array of selected choices. The format of the array items depends on the Output Format setting.

Was this article helpful?