Skip to content

Select Field

A dropdown select input for choosing a single option from a list.

Overview

The Select field renders a searchable dropdown input and returns the selected option value as a string. Use it for layout choices, style presets, position settings, post selections, or any option where the user needs to pick one item from a defined list.


Field Registration

php

[
    'id'       => 'header_layout',
    'type'     => 'select',
    'title'    => __('Header Layout', 'your-textdomain'),
    'subtitle' => __('Choose the header style', 'your-textdomain'),
    'default'  => 'standard',
    'options'  => [
        'standard'  => __('Standard', 'your-textdomain'),
        'centered'  => __('Centered', 'your-textdomain'),
        'fullwidth' => __('Full Width', 'your-textdomain'),
        'minimal'   => __('Minimal', 'your-textdomain'),
    ],
]

Field Options

OptionTypeRequiredDescription
idstringUnique field identifier — used as the option key
typestringMust be select
titlestringLabel shown above the field
subtitlestringSmaller descriptive text shown below the label
descstringHelp text shown below the input
defaultstringDefault selected value — must match a key in options
optionsarrayKey-value pairs — key is stored, value is the label shown
requiredarrayConditional logic rules — see Conditional Logic

Return Value

Type: string

Returns the key of the selected option, or the default value if nothing has been saved yet.

php

$layout = themeplus_get_option( 'header_layout', 'standard' );
// Returns: 'standard'

Usage Examples

Applying a CSS class

php

$header_layout = themeplus_get_option( 'header_layout', 'standard' );

echo '<header class="site-header site-header--' . esc_attr( $header_layout ) . '">';

Mapping value to a label

php

$layout = themeplus_get_option( 'header_layout', 'standard' );

$labels = [
    'standard'  => 'Standard',
    'centered'  => 'Centered',
    'fullwidth' => 'Full Width',
    'minimal'   => 'Minimal',
];

echo esc_html( $labels[ $layout ] ?? $layout );

Conditional template logic

php

$blog_style = themeplus_get_option( 'blog_style', 'grid' );

if ( $blog_style === 'grid' ) {
    get_template_part( 'template-parts/blog/grid' );
} elseif ( $blog_style === 'list' ) {
    get_template_part( 'template-parts/blog/list' );
} else {
    get_template_part( 'template-parts/blog/masonry' );
}

Dynamically populated options

php

// Populate with registered post types
$post_types = get_post_types( ['public' => true], 'objects' );
$options    = [];

foreach ( $post_types as $post_type ) {
    $options[ $post_type->name ] = $post_type->label;
}

themeplus_add_section([
    'id'     => 'blog',
    'title'  => __('Blog', 'your-textdomain'),
    'icon'   => 'fa-solid fa-newspaper',
    'fields' => [
        [
            'id'      => 'portfolio_post_type',
            'type'    => 'select',
            'title'   => __('Portfolio Post Type', 'your-textdomain'),
            'options' => $options,
            'default' => 'post',
        ],
    ],
]);

With a conditional field

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'),
        '4-col' => __('4 Columns', 'your-textdomain'),
    ],
],
[
    'id'       => 'footer_widget_title',
    'type'     => 'text',
    'title'    => __('Widget Area Title', 'your-textdomain'),
    'required' => ['footer_layout', '!=', '1-col'],
],

Notes

  • The options array keys are what get stored in the database and returned by themeplus_get_option() — keep them lowercase, hyphenated, and stable across theme versions.
  • Always sanitize the returned value before using it in class names or template logic — use esc_attr() for HTML attributes and sanitize_key() for internal comparisons.
  • For a visual button-based alternative to a dropdown, use the Button Set field.
  • For choosing between options with images, use the Select Image field.
  • For allowing multiple selections, use the Checkbox field.

On This Page