wp-settings-builder

5. Fields: Choice and Toggles

This guide covers fields designed for making selections, from simple on/off toggles to complex, searchable multi-select dropdowns.

A key extra for many of these fields is the 'options' array. This is an associative array where the key is the value that gets saved to the database, and the value is the user-facing label.

'options' => [
    'low'    => 'Low Priority',
    'medium' => 'Medium Priority',
    'high'   => 'High Priority',
]

Checkbox

Example

$page->add_field( 
    'open_links_in_new_tab', 
    'general_section', 
    'checkbox', 
    'New Tab Behavior', 
    [
        'description' => 'Enable this to open all external links in a new browser tab.',
        'default'     => true,
    ]
);

Switch

Example

$page->add_field( 
    'enable_dark_mode', 
    'display_section', 
    'switch', 
    'Dark Mode', 
    [
        'description' => 'Activate the dark mode theme for the front-end.',
        'default'     => false,
    ]
);

Choice (Radio Buttons)

Example

$page->add_field( 
    'thumbnail_position', 
    'display_section', 
    'choice', 
    'Thumbnail Position', 
    [
        'description' => 'Choose where to display the featured image on single posts.',
        'default'     => 'above_title',
        'options'     => [
            'above_title' => 'Above the Post Title',
            'below_title' => 'Below the Post Title',
            'no_thumb'    => 'Do Not Display',
        ],
    ]
);

Buttons Group

Example

$page->add_field( 
    'content_alignment', 
    'display_section', 
    'buttons_group', 
    'Content Alignment', 
    [
        'description' => 'Select the default text alignment for your content.',
        'default'     => 'left',
        'options'     => [
            'left'   => 'Left',
            'center' => 'Center',
            'right'  => 'Right',
        ],
    ]
);

Select

Example

$page->add_field( 
    'font_selection', 
    'display_section', 
    'select', 
    'Primary Font', 
    [
        'description' => 'Choose a font from the Google Fonts library.',
        'default'     => 'roboto',
        'options'     => [
            'roboto'    => 'Roboto',
            'open_sans' => 'Open Sans',
            'lato'      => 'Lato',
            'montserrat'=> 'Montserrat',
        ],
    ]
);

Multi-Check

Example

$page->add_field( 
    'post_types_to_search',
    'general_section', 
    'multi_check', 
    'Searchable Content', 
    [
        'description' => 'Select which post types should be included in search results.',
        'default'     => ['post', 'page'],
        'options'     => [
            'post'    => 'Posts',
            'page'    => 'Pages',
            'product' => 'Products',
            'docs'    => 'Documentation',
        ],
    ]
);

Multi-Select

Example

$page->add_field( 
    'user_roles_access', 
    'advanced_section', 
    'multi_select', 
    'Roles with Access', 
    [
        'description' => 'Select which user roles can access the premium features.',
        'default'     => ['administrator', 'editor'],
        'options'     => [
            'administrator' => 'Administrator',
            'editor'        => 'Editor',
            'author'        => 'Author',
            'contributor'   => 'Contributor',
            'subscriber'    => 'Subscriber',
        ],
    ]
);

Now that you’ve mastered choice-based fields, the next section will cover fields for handling rich content, code, and media from the WordPress library.

Next: 6. Fields: Content and Media