Skip to content

Field example: Select

Morgan Estes edited this page Jun 11, 2015 · 4 revisions

Here's how you can use a select field with pre-populated options within the registration of your Shortcode UI.

array(
    'label' => 'Field Label',
    'attr' => 'yourshortcodeattribute',
    'type' => 'select',
    'options' => array(
        'value1' => 'Label 1',
        'value2' => 'Label 2',
        'value3' => 'Label 3',
    ),
),

If you're using select for number range, you can shorten the 'options' value:

array(
    'label'    => __( 'Number of Items', 'your-text-domain' ),
    'attr'     => 'items',
    'type'     => 'select',
    'options'  => array_combine( range( 1, 7 ), range( 1, 7 ) ),
    'multiple' => false,
    ),

produces:

<select name="items" id="shortcode-ui-items-c300" placeholder="">
  <option value="1" selected="">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  <option value="7">7</option>
</select>
Clone this wiki locally