Skip to content

Toggle Field

An on/off switch for enabling or disabling a feature.

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

OptionTypeRequiredDescription
idstringUnique field identifier — used as the option key
typestringMust be toggle or switch
titlestringLabel shown beside the toggle
subtitlestringSmaller descriptive text shown below the label
descstringHelp text shown below the toggle
defaultboolDefault state — true for on, false for off
requiredarrayConditional 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 required on 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 in if statements without casting.
  • Both toggle and switch type aliases produce the same field component.

On This Page