Overview
The Toggle field renders a switch-style on/off control and returns a boolean value. Use it for any binary setting — enabling or disabling features, showing or hiding elements, switching between two states. It can also be used as switch — both type aliases map to the same component.
Field Registration
php
[
'id' => 'enable_preloader',
'type' => 'toggle',
'title' => __('Enable Preloader', 'your-textdomain'),
'subtitle' => __('Show a preloader animation while the page loads', 'your-textdomain'),
'default' => true,
]
Field Options
| Option | Type | Required | Description |
|---|---|---|---|
id | string | ✅ | Unique field identifier — used as the option key |
type | string | ✅ | Must be toggle or switch |
title | string | ✅ | Label shown beside the toggle |
subtitle | string | — | Smaller descriptive text shown below the label |
desc | string | — | Help text shown below the toggle |
default | bool | — | Default state — true for on, false for off |
required | array | — | Conditional logic rules — see Conditional Logic |
Return Value
Type: bool
Returns true when the toggle is on, false when off, or the default value if nothing has been saved yet.
php
$enabled = themeplus_get_option( 'enable_preloader', true );
// Returns: true or false
Usage Examples
Basic on/off check
php
$enable_preloader = themeplus_get_option( 'enable_preloader', true );
if ( $enable_preloader ) {
get_template_part( 'template-parts/preloader' );
}
Showing or hiding an element
php
$show_back_to_top = themeplus_get_option( 'show_back_to_top', true );
if ( $show_back_to_top ) {
echo '<button class="back-to-top" aria-label="Back to top">';
echo '<i class="fa-solid fa-arrow-up"></i>';
echo '</button>';
}
Adding a body class
php
$dark_mode = themeplus_get_option( 'enable_dark_mode', false );
add_filter( 'body_class', function( $classes ) use ( $dark_mode ) {
if ( $dark_mode ) {
$classes[] = 'dark-mode';
}
return $classes;
});
Enqueuing a script conditionally
php
$enable_animations = themeplus_get_option( 'enable_animations', true );
if ( $enable_animations ) {
wp_enqueue_script( 'gsap', get_template_directory_uri() . '/assets/js/gsap.min.js', [], '3.0', true );
}
As a conditional logic trigger
php
[
'id' => 'enable_preloader',
'type' => 'toggle',
'title' => __('Enable Preloader', 'your-textdomain'),
'default' => true,
],
[
'id' => 'preloader_style',
'type' => 'radio',
'title' => __('Preloader Style', 'your-textdomain'),
'default' => 'spinner',
'options' => [
'spinner' => __('Spinner', 'your-textdomain'),
'logo' => __('Logo', 'your-textdomain'),
'progress' => __('Progress Bar', 'your-textdomain'),
],
'required' => ['enable_preloader', '==', true],
],
[
'id' => 'preloader_bg_color',
'type' => 'color',
'title' => __('Preloader Background', 'your-textdomain'),
'default' => '#ffffff',
'required' => ['enable_preloader', '==', true],
],
Notes
- The Toggle field is the most common trigger for conditional logic in ThemePlus. Pair it with
requiredon dependent fields to show or hide entire groups of options based on a single switch. - The field returns a native PHP
bool— use it directly inifstatements without casting. - Both
toggleandswitchtype aliases produce the same field component.