Fields / Select

Read More: Fieldmanager_Select

Select dropdowns.

This class extends Fieldmanager_Options, which allows you to define options (values) via an array or via a dynamic Datasource, like posts, terms, or users.

  1. Example 1: Basic Select Field

    add_action( 'fm_post_post', function() {
    	$fm = new Fieldmanager_Select( array(
    		'name' => 'demo-field',
    		'options' => array(
    			'red' => 'Red',
    			'green' => 'Green',
    			'blue' => 'Blue',
    		),
    	) );
    	$fm->add_meta_box( 'Select Demo', array( 'post' ) );
    } );
  2. Example 2: First Empty

    Setting the first_empty option to true will add an empty value in front of your options, so one is not selected by default. This is most useful when using a Datasource, where you're not necessarily able to control the list of data you're displaying.

    add_action( 'fm_post_post', function() {
    	$fm = new Fieldmanager_Select( array(
    		'name' => 'demo-field',
    		'first_empty' => true,
    		'options' => array(
    			'red' => 'Red',
    			'green' => 'Green',
    			'blue' => 'Blue',
    		),
    	) );
    	$fm->add_meta_box( 'Select Demo', array( 'post' ) );
    } );
  3. Example 3: Datasource Example

    This example will output a select dropdown with all users on the site. To provide the field with that data, it uses the User Datasource.

    add_action( 'fm_post_post', function() {
    	$fm = new Fieldmanager_Select( array(
    		'name' => 'demo-field',
    		'datasource' => new Fieldmanager_Datasource_User,
    	) );
    	$fm->add_meta_box( 'Select Demo', array( 'post' ) );
    } );
  4. Example 4: Multiple Select Field

    This example will add a multiple-select field.

    Multiple-select boxes are not always intuitive to use, so other alternatives should probably be considered. As noted on w3schools.com:

    Selecting multiple options vary in different operating systems and browsers:

    • For windows: Hold down the control (ctrl) button to select multiple options
    • For Mac: Hold down the command button to select multiple options

    Because of the different ways of doing this, and because you have to inform the user that multiple selection is available, it is more user-friendly to use checkboxes instead.

    add_action( 'fm_post_post', function() {
    	$fm = new Fieldmanager_Select( array(
    		'name' => 'demo-field',
    		'multiple' => true,
    		'attributes' => array(
    			'size' => 3,
    		),
    		'options' => array(
    			'red' => 'Red',
    			'green' => 'Green',
    			'blue' => 'Blue',
    		),
    	) );
    	$fm->add_meta_box( 'Select Demo', array( 'post' ) );
    } );