Skip to content

Select Field

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

A searchable dropdown for choosing a single option from a list.

Overview

The Select field renders a searchable dropdown and returns the selected option’s key as a string. Use it for layout choices, style presets, position settings, sidebar positions, or any option where the user picks one item from a defined list.

For a visual button-based alternative, use the Button Set field. For options with preview images, use the Select Image field. For multiple selections, use the Checkbox field.

Field Registration

[
    '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
subtitlestringDescriptive text shown below the label
descstringAlternative to subtitle — shown in the same position
optionsarray'key' => 'Label' map — key is stored, label is shown in the dropdown
defaultstringDefault selected value — must match a key in options
placeholderstringPlaceholder text shown when no option is selected
disabledboolDisables the dropdown when true. Default: false
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. Returns an empty string '' if no default is set and nothing has been saved.

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

Usage Examples

Applying a CSS modifier class

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

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

Conditional template logic

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

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

Mapping value to a display label

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

$labels = [
    'standard'  => __( 'Standard', 'your-textdomain' ),
    'centered'  => __( 'Centered', 'your-textdomain' ),
    'fullwidth' => __( 'Full Width', 'your-textdomain' ),
    'minimal'   => __( 'Minimal', 'your-textdomain' ),
];

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

Dynamically populated options

// Build options from registered public 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'     => 'portfolio',
    'title'  => __( 'Portfolio', 'your-textdomain' ),
    'icon'   => 'briefcase',
    'fields' => [
        [
            'id'      => 'portfolio_post_type',
            'type'    => 'select',
            'title'   => __( 'Portfolio Post Type', 'your-textdomain' ),
            'options' => $options,
            'default' => 'post',
        ],
    ],
]);

With a conditional field

[
    '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. Changing a key in a future version will cause saved values to no longer match.
  • The dropdown includes a built-in search — useful when the options list is long.
  • Always use esc_attr() when using the returned value in an HTML attribute, or compare strictly with === in PHP logic.
  • For a button-group visual alternative, use the Button Set field. For options with preview images, use the Select Image field. For multiple selections, use the Checkbox field.

On This Page