vunamhung/cmb2_select2

This is a CMB2 Extension repository which help modify the default behavior of CMB2 and extends it's functionality.

1.0.2 2020-02-26 03:20 UTC

This package is auto-updated.

Last update: 2024-05-12 05:20:21 UTC


README

Description

Select2 field type for CMB2.

This plugin gives you two additional field types based on Select2:

  1. The select2 field acts much like the default select field. However, it adds typeahead-style search allowing you to quickly make a selection from a large list
  2. The multiselect2 field allows you to select multiple values with typeahead-style search. The values can be dragged and dropped to reorder

Usage

select2 - Select box with with typeahead-style search. Example:

$cmb->add_field( array(
	'name'    => 'Cooking time',
	'id'      => $prefix . 'cooking_time',
	'desc'    => 'Cooking time',
	'type'    => 'select2',
	'options' => array(
		'5'  => '5 minutes',
		'10' => '10 minutes',
		'30' => 'Half an hour',
		'60' => '1 hour',
	),
) );

multiselect2 - Multi-value select box with drag and drop reordering. Example:

$cmb->add_field( array(
	'name'    => 'Ingredients',
	'id'      => $prefix . 'ingredients',
	'desc'    => 'Select ingredients. Drag to reorder.',
	'type'    => 'multiselect2',
	'options' => array(
		'flour'  => 'Flour',
		'salt'   => 'Salt',
		'eggs'   => 'Eggs',
		'milk'   => 'Milk',
		'butter' => 'Butter',
	),
) );

Placeholder

You can specify placeholder text through the attributes array. Example:

$cmb->add_field( array(
	'name'    => 'Ingredients',
	'id'      => $prefix . 'ingredients',
	'desc'    => 'Select this recipes ingredients.',
	'type'    => 'multiselect2',
	'options' => array(
		'flour'  => 'Flour',
		'salt'   => 'Salt',
		'eggs'   => 'Eggs',
		'milk'   => 'Milk',
		'butter' => 'Butter',
	),
	'attributes' => array(
		'placeholder' => 'Select ingredients. Drag to reorder'
	),
) );

Custom Select2 configuration and overriding default configuration options

You can define Select2 configuration options using HTML5 data-* attributes. It's worth reading up on the available options over on the Select2 website. Example:

$cmb->add_field( array(
	'name'    => 'Ingredients',
	'id'      => $prefix . 'ingredients',
	'desc'    => 'Select ingredients. Drag to reorder.',
	'type'    => 'multiselect2',
	'options' => array(
		'flour'  => 'Flour',
		'salt'   => 'Salt',
		'eggs'   => 'Eggs',
		'milk'   => 'Milk',
		'butter' => 'Butter',
	),
	'attributes' => array(
		'data-maximum-selection-length' => '2',
	),
) );