bookmark_borderselect Field

Introduction

The select type field, part of the Settings API, allows the user to select a choice from a drop down.

Example

The following example shows a section with two select type fields. The first is a simple select with two choices. The second select field has two choices with the first choice having sub-choices (an option group).

array(
	'title'       => esc_html__( 'This is the title for Section 1', 'sometextdomain' ),
	'description' => esc_html__( 'This is a description of the purpose of Section 1', 'sometextdomain' ),
	'fields'      => array(
		array(
			'type'    => 'select',
			'name'    => 'myselect',
			'label'   => esc_html__( 'This is a select', 'sometextdomain' ),
			'choices' => array(
				array(
					'label' => esc_html__( 'my first choice', 'sometextdomain' ),
					'value' => '1'
				),
				array(
					'label' => esc_html__( 'my second choice', 'sometextdomain' ),
					'value' => '2'
				)
			)
		),
		array(
			'type'          => 'select',
			'name'          => 'myselect2',
			'label'         => esc_html__( 'This is my select', 'sometextdomain' ),
			'default_value' => 'first - 3',
			'choices'       => array(
				array(
					'label'   => esc_html__( 'This is my first choice', 'sometextdomain' ),
					'value'   => 'first',
					'choices' => array(
						array(
							'label' => esc_html__( 'first - 1', 'sometextdomain' ),
							'value' => 'f1'
						),
						array(
							'label' => esc_html__( 'first - 2', 'sometextdomain' ),
							'value' => 'f2',
						),
						array(
							'label' => esc_html__( 'first - 3', 'sometextdomain' ),
						),
					),
				),
				array(
					'label' => esc_html__( 'This is my second choice', 'sometextdomain' ),
					'value' => 'second',
				),
			),
		)
	),
),

The code above will render the select fields similar to the following:
Select Field Example

Uses