Skip to content

Checkbox Field

A group of checkboxes for selecting one or more options from a list.

Overview

The Checkbox field renders a list of checkboxes where any number can be selected at once. It returns an array of the selected option keys. Use it when users need to enable multiple features simultaneously — supported elements, visible sections, active modules, or any other multi-select scenario.


Field Registration

php

[
    'id'       => 'header_elements',
    'type'     => 'checkbox',
    'title'    => __('Header Elements', 'your-textdomain'),
    'subtitle' => __('Choose which elements to show in the header', 'your-textdomain'),
    'default'  => ['logo', 'navigation', 'search'],
    'options'  => [
        'logo'       => __('Logo', 'your-textdomain'),
        'navigation' => __('Navigation', 'your-textdomain'),
        'search'     => __('Search', 'your-textdomain'),
        'cart'       => __('Cart Icon', 'your-textdomain'),
        'social'     => __('Social Icons', 'your-textdomain'),
    ],
]

Field Options

OptionTypeRequiredDescription
idstringUnique field identifier — used as the option key
typestringMust be checkbox
titlestringLabel shown above the field
subtitlestringSmaller descriptive text shown below the label
descstringHelp text shown below the checkboxes
defaultarrayArray of keys selected by default
optionsarrayKey-value pairs — key is stored, value is the label shown
requiredarrayConditional logic rules — see Conditional Logic

Return Value

Type: array

Returns an array of the selected option keys, or the default array if nothing has been saved yet. Returns an empty array if nothing is selected.

php

$elements = themeplus_get_option( 'header_elements', ['logo', 'navigation', 'search'] );
// Returns: ['logo', 'navigation', 'search']

Usage Examples

Checking if a specific item is selected

php

$elements = themeplus_get_option( 'header_elements', [] );

if ( in_array( 'search', $elements, true ) ) {
    get_template_part( 'template-parts/header/search' );
}

if ( in_array( 'cart', $elements, true ) ) {
    get_template_part( 'template-parts/header/cart' );
}

Looping through selected values

php

$features = themeplus_get_option( 'enabled_features', [] );

if ( ! empty( $features ) ) {
    echo '<ul class="feature-list">';
    foreach ( $features as $feature ) {
        echo '<li>' . esc_html( $feature ) . '</li>';
    }
    echo '</ul>';
}

Applying CSS classes from selections

php

$body_classes = themeplus_get_option( 'body_classes', [] );

add_filter( 'body_class', function( $classes ) use ( $body_classes ) {
    if ( is_array( $body_classes ) ) {
        foreach ( $body_classes as $class ) {
            $classes[] = sanitize_html_class( $class );
        }
    }
    return $classes;
});

Social networks to display

php

[
    'id'      => 'social_networks',
    'type'    => 'checkbox',
    'title'   => __('Social Networks', 'your-textdomain'),
    'default' => ['facebook', 'twitter', 'instagram'],
    'options' => [
        'facebook'  => __('Facebook', 'your-textdomain'),
        'twitter'   => __('Twitter / X', 'your-textdomain'),
        'instagram' => __('Instagram', 'your-textdomain'),
        'linkedin'  => __('LinkedIn', 'your-textdomain'),
        'youtube'   => __('YouTube', 'your-textdomain'),
        'pinterest' => __('Pinterest', 'your-textdomain'),
    ],
]

php

$networks = themeplus_get_option( 'social_networks', [] );

if ( ! empty( $networks ) ) {
    echo '<div class="social-icons">';
    foreach ( $networks as $network ) {
        echo '<a href="#" class="social-icon social-icon--' . esc_attr( $network ) . '">';
        echo '<i class="fa-brands fa-' . esc_attr( $network ) . '"></i>';
        echo '</a>';
    }
    echo '</div>';
}

With conditional logic using contains

php

[
    'id'      => 'header_elements',
    'type'    => 'checkbox',
    'title'   => __('Header Elements', 'your-textdomain'),
    'default' => ['logo', 'navigation'],
    '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 Text', 'your-textdomain'),
    'default'  => __('Search...', 'your-textdomain'),
    'required' => ['header_elements', 'contains', 'search'],
],

Notes

  • Always check is_array() before looping through the returned value — if the option has never been saved, the default may be an empty array or not set at all.
  • Use in_array( $value, $array, true ) with strict comparison (true) for reliable checks.
  • The contains and !contains conditional logic operators work directly with Checkbox field values — see Conditional Logic for details.
  • For selecting only one option at a time, use the Radio or Button Set field instead.

On This Page