How to enable Select2 on select dropdowns?

Since version 3.3, it is now possible to turn any custom fields “select” into a Select2 Dropdown. This is particularly useful for dropdowns with lots of items.

Enable it for custom fields

It couldn’t be easier to enable Select2 for your custom fields. Simply add the argument select2 => true to any custom field (which type is select or taxonomy).

Enable it for “core” dropdowns (ie Products, Department)

To enable select2 for the products dropdown, we prepare a code snippet for you that uses the built-in filters:

 

DATA HOSTED WITH ♥ BY PASTEBIN.COM – DOWNLOAD RAW – SEE ORIGINAL
  1. add_filter( ‘wpas_get_custom_fields’, ‘wpas_products_dropdown_select2’, 10, 1 );
  2. /**
  3.  * Enable Select2 for the “Products” dropdown
  4.  *
  5.  * @param array $fields Registered custom fields
  6.  * @return array
  7.  */
  8. function wpas_products_dropdown_select2( $fields ) {
  9.     if ( isset( $fields[‘product’] ) ) {
  10.         $fields[‘product’][‘args’][‘select2’] = true;
  11.     }
  12.     return $fields;
  13. }

 

 

Smart Chat
Smart Replies add-on Chat X