Skip to content

Conditional Logic

Show or hide fields dynamically based on the value of another field.

Overview

Conditional logic lets you control the visibility of any field based on the current value of another field in the same section. When a condition is met the field is shown — when it is not met the field is hidden. This keeps the options panel clean and focused, surfacing only the settings that are relevant to the user’s current choices.

Conditions are defined using the required key on any field registration array.


Basic Syntax

php

'required' => [ 'field_id', 'operator', 'value' ]
PartTypeDescription
field_idstringThe id of the controlling field in the same section
operatorstringThe comparison operator — see the full list below
valuemixedThe value to compare against — type must match the controlling field’s return type

Operators

OperatorDescriptionExample controlling field types
==Equal toToggle, Select, Button Set, Radio, Text
!=Not equal toToggle, Select, Button Set, Radio, Text
>Greater thanNumber, Slider
>=Greater than or equal toNumber, Slider
<Less thanNumber, Slider
<=Less than or equal toNumber, Slider
containsArray contains valueCheckbox
!containsArray does not contain valueCheckbox
emptyValue is emptyText, Textarea, Image
!emptyValue is not emptyText, Textarea, Image

Usage Examples

Toggle — show fields when a feature is enabled

The most common pattern. A Toggle controls whether a group of dependent fields is visible.

php

[
    'id'      => 'enable_preloader',
    'type'    => 'toggle',
    'title'   => __('Enable Preloader', 'your-textdomain'),
    'default' => false,
],
[
    '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],
],

Select / Button Set — show fields based on the chosen option

php

[
    'id'      => 'header_style',
    'type'    => 'button_set',
    'title'   => __('Header Style', 'your-textdomain'),
    'default' => 'light',
    'options' => [
        'light'  => __('Light', 'your-textdomain'),
        'dark'   => __('Dark', 'your-textdomain'),
        'custom' => __('Custom', 'your-textdomain'),
    ],
],
[
    'id'       => 'header_bg_color',
    'type'     => 'color',
    'title'    => __('Header Background', 'your-textdomain'),
    'default'  => '#ffffff',
    'required' => ['header_style', '==', 'custom'],
],
[
    'id'       => 'header_text_color',
    'type'     => 'color',
    'title'    => __('Header Text Color', 'your-textdomain'),
    'default'  => '#1e1e1e',
    'required' => ['header_style', '==', 'custom'],
],

Chained conditions — a field depends on two levels

php

[
    'id'      => 'enable_hero',
    'type'    => 'toggle',
    'title'   => __('Enable Hero Section', 'your-textdomain'),
    'default' => true,
],
[
    'id'       => 'hero_type',
    'type'     => 'button_set',
    'title'    => __('Hero Type', 'your-textdomain'),
    'default'  => 'image',
    'options'  => [
        'image' => __('Image', 'your-textdomain'),
        'video' => __('Video', 'your-textdomain'),
        'slider' => __('Slider', 'your-textdomain'),
    ],
    'required' => ['enable_hero', '==', true],
],
[
    'id'       => 'hero_image',
    'type'     => 'image',
    'title'    => __('Hero Image', 'your-textdomain'),
    'required' => ['hero_type', '==', 'image'],
],
[
    'id'       => 'hero_video_url',
    'type'     => 'text',
    'title'    => __('Video URL', 'your-textdomain'),
    'required' => ['hero_type', '==', 'video'],
],

Number / Slider — show fields based on a numeric threshold

php

[
    'id'      => 'sidebar_width',
    'type'    => 'slider',
    'title'   => __('Sidebar Width', 'your-textdomain'),
    'default' => 300,
    'min'     => 200,
    'max'     => 500,
    'step'    => 10,
    'unit'    => 'px',
],
[
    'id'       => 'sidebar_wide_notice',
    'type'     => 'info',
    'style'    => 'warning',
    'desc'     => __('A sidebar wider than 400px may affect readability on smaller screens.', 'your-textdomain'),
    'required' => ['sidebar_width', '>', 400],
],

Checkbox — show a field when a specific item is checked

php

[
    'id'      => 'header_elements',
    'type'    => 'checkbox',
    'title'   => __('Header Elements', 'your-textdomain'),
    'default' => ['logo', 'navigation', 'search'],
    'options' => [
        'logo'       => __('Logo', 'your-textdomain'),
        'navigation' => __('Navigation', 'your-textdomain'),
        'search'     => __('Search', 'your-textdomain'),
        'cart'       => __('Cart Icon', 'your-textdomain'),
    ],
],
[
    'id'       => 'search_placeholder',
    'type'     => 'text',
    'title'    => __('Search Placeholder', 'your-textdomain'),
    'default'  => __('Search...', 'your-textdomain'),
    'required' => ['header_elements', 'contains', 'search'],
],
[
    'id'       => 'cart_icon_style',
    'type'     => 'button_set',
    'title'    => __('Cart Icon Style', 'your-textdomain'),
    'default'  => 'icon',
    'options'  => [
        'icon'      => __('Icon Only', 'your-textdomain'),
        'icon_text' => __('Icon + Count', 'your-textdomain'),
    ],
    'required' => ['header_elements', 'contains', 'cart'],
],

Text / Image — show a field when another field is not empty

php

[
    'id'    => 'custom_badge_text',
    'type'  => 'text',
    'title' => __('Badge Text', 'your-textdomain'),
],
[
    'id'       => 'custom_badge_color',
    'type'     => 'color',
    'title'    => __('Badge Color', 'your-textdomain'),
    'default'  => '#d63638',
    'required' => ['custom_badge_text', '!empty', ''],
],

Using != to hide a field for one specific value

php

[
    'id'      => 'footer_layout',
    'type'    => 'select',
    'title'   => __('Footer Layout', 'your-textdomain'),
    'default' => '3-col',
    'options' => [
        '1-col' => __('1 Column', 'your-textdomain'),
        '2-col' => __('2 Columns', 'your-textdomain'),
        '3-col' => __('3 Columns', 'your-textdomain'),
    ],
],
[
    'id'       => 'footer_widget_title',
    'type'     => 'text',
    'title'    => __('Widget Area Title', 'your-textdomain'),
    'required' => ['footer_layout', '!=', '1-col'],
],

Multiple Conditions (AND logic)

To require multiple conditions simultaneously, pass an array of condition arrays. All conditions must be true for the field to be shown.

php

[
    'id'       => 'hero_overlay_color',
    'type'     => 'color',
    'title'    => __('Overlay Color', 'your-textdomain'),
    'default'  => 'rgba(0,0,0,0.5)',
    'required' => [
        ['enable_hero',      '==', true    ],
        ['hero_type',        '==', 'image' ],
    ],
],

Notes

  • The field_id in required must reference a field in the same section — cross-section conditional logic is not supported.
  • Conditional logic controls visibility only — hidden fields are not cleared or reset when their condition becomes false. Their saved values are preserved in the database.
  • The empty and !empty operators compare the controlling field’s value against an empty string — pass an empty string '' as the value argument: ['my_field', '!empty', ''].
  • For Toggle fields, always pass a native PHP boolean (true or false) as the comparison value, not a string ('true' or 'false').
  • Conditional logic runs entirely in the browser — no page reload is required when a controlling field value changes.

On This Page