Skip to content

Multiselect

Radoslav Georgiev edited this page Oct 21, 2018 · 3 revisions

Purpose

The Multiselect field in Ultimate Fields builds on top of the select field while allowing users to select multiple options. Because of this, there are changes in the inputs of the field:

  • Instead of a standard <select> element, this field adds the multiple attribute in order to allow multiple options to be selected. In this mode the Select2 plugin is always enabled, as standard browser multi-selects are extremely user-unfriendly.
  • Instead of the radio input type of the Select field, the Multiselect introduces checkbox as an input type.

multiselect-field

Options

The Multiselect field is based on and extends the Select Field. Please read that article to learn how to add options to the multiselect field.

Input Type

As shown in the screenshot in the beginning of this article, the multiselect field supports two input types:

  • A multiselect select, as indicated in the name of the field, is the default input type.
  • Checkboxes will use <input type="checkbox" />-es for each option and is more suitable for fields with few options.

In PHP, you can use the set_input_type( $type) method of the field. The supported options for $type are "multiselect" (defult) and "checkboxes".

Field::create( 'multiselect', 'modules' )
	->set_input_type( 'checkboxes' )
	->add_options(array(
		'intro'            => 'Intro',
		'copy'             => 'Copy',
		'related_articles' => 'Related articles'
	))

Usage

<?php
$modules = get_value( 'modules' );

if( in_array( 'intro', $modules ) ) { ?>
	<!-- Intro -->
<?php }

if( in_array( 'copy', $modules ) ) { ?>
	<!-- Copy -->
<?php }

if( in_array( 'related_articles', $modules ) ) { ?>
	<!-- Related articles -->
<?php }
Clone this wiki locally