A Cozmoslabs Product
Documentation / Developer Knowledge Base / Custom Fields / Populate Select 2 fields from the database

Populate Select 2 fields from the database

Populating a Select2 field from the database can be done with custom code. Below we have an example:

add_filter('wppb_select2_labels_array', 'wppbc_custom_select_values', 10, 5); 
add_filter('wppb_select2_options_array', 'wppbc_custom_select_values', 10, 5); 
function wppbc_custom_select_values($values, $field, $form_location, $user_id, $request_data) {   

   if ($field['meta-name'] == 'my_custom_code_select') {      
      $values = array('one', 'two', 'three');     
   }      

   return $values;

}

You will need to replace my_custom_code_select with the meta name of the field you want to target. Then, retrieve the values you want to display and return them in an array.


To have different labels and values, you need to hook a separate function on each filter.

Profile Builder Pro

Create beautiful front-end registration and profile forms with custom fields. Setup member directories, custom redirects, cutomize user emails & more using the all in one user management plugin.

Get Profile Builder

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket