Title: Populate select menus from function
Last modified: October 10, 2018

---

# Populate select menus from function

 *  Resolved [captaincainer](https://wordpress.org/support/users/captaincainer/)
 * (@captaincainer)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/populate-select-menus-from-function/)
 * Hi,
    Is it possible to create the options for a select menu via a function? I
   have a ton of options and the click to add isn’t very developer friendly.
 * Thanks,
    Aaron.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Contributor [Alessandro Tesoro](https://wordpress.org/support/users/alessandrotesoro/)
 * (@alessandrotesoro)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/populate-select-menus-from-function/#post-10770336)
 * Hi Aaron,
 * If you need to add options “dynamically” to a dropdown, you can use the built-
   in filters of the plugin to modify fields.
 * Is it a profile or registration form field?
 * Let me know so I can tell you which filter to use.
 *  Thread Starter [captaincainer](https://wordpress.org/support/users/captaincainer/)
 * (@captaincainer)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/populate-select-menus-from-function/#post-10770377)
 * Both Types,
 * I have a country dropdown that needs to be populated by a “country” post_type
   and a “specialisation” dropdown that needs to be populated from a meta_value.
 * I’m currently using jQuery but would be happier if there was a built in filter,
   function or hook.
 * This is my current jquery and php for the country dropdown.
 *     ```
       <?php
        // query for your post type
       $post_type_query  = new WP_Query(  
       	array (  
       		'post_type'		=> 'country',  
       		'posts_per_page'	=> -1 , 
       		'orderby'		=> 'title',
       		'order'			=> 'ASC',
       	)  
       );   
       // we need the array of posts
       $posts_array = $post_type_query->posts;   
       // create a list with needed information
       // the key equals the ID, the value is the post_title
       $post_title_array = wp_list_pluck( $posts_array, 'post_title', 'post_name' );
       //echo '<pre>' . var_export(json_encode($post_title_array), true) . '</pre>';
       ?>
   
       <script>
       (function($) {
       	var selectValues = <?php echo json_encode($post_title_array); ?>;
       	var selectMenu = $('#country-select');
   
       	$.each(selectValues, function(key, value) {
       		selectMenu
       		  .append($('<option>', { value : key })
       		  .text(value));
       	});
       })(jQuery);
       </script>
       ```
   
 *  Plugin Contributor [Alessandro Tesoro](https://wordpress.org/support/users/alessandrotesoro/)
 * (@alessandrotesoro)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/populate-select-menus-from-function/#post-10773877)
 * Hi Aaron,
 * Yes there’s filters, so you don’t have to use jquery.
 * The following snippet is for the registration form, you can add the snippet into
   your theme’s functions.php file.
 *     ```
       function wpum_modify_registration_field( $fields ) {
   
       	$fields['my_field_id_here']['options'] = [
       		'opt_val' => 'option label',
       	];
   
       	return $fields;
   
       }
       add_filter( 'wpum_get_registration_fields', 'wpum_modify_registration_field', 20 );
       ```
   
 * You need to replace `my_field_id_here` with the id of the field you wish to customize.
   The easiest way to find out the id is by printing out the `$fields` variable.
   You’ll see the associative array that is loaded into the form. Here’s a screenshot
   so you can see what the id is [https://www.dropbox.com/s/1tmay85ju6n7oqj/Schermata%202018-10-12%20alle%2010.44.49.png?dl=0](https://www.dropbox.com/s/1tmay85ju6n7oqj/Schermata%202018-10-12%20alle%2010.44.49.png?dl=0)
 * Once you’ve found your id, you can load your custom options within the array.
 * For the account form, it looks like I forgot to add a filter there, so I’ve added
   a filter just now so you need to manually update a core file of the plugin ( 
   I’ll release an official update at a later point ) – this is the file [https://github.com/WPUserManager/wp-user-manager/blob/master/includes/wpum-forms/class-wpum-form-profile.php#L198](https://github.com/WPUserManager/wp-user-manager/blob/master/includes/wpum-forms/class-wpum-form-profile.php#L198)
 * Once you’ve replaced the file, you can use the exact same code from the snippet
   above and in this case, use the filter ‘wpum_get_account_fields’.
 * Hope this helps 🙂
    -  This reply was modified 7 years, 7 months ago by [Alessandro Tesoro](https://wordpress.org/support/users/alessandrotesoro/).
 *  Thread Starter [captaincainer](https://wordpress.org/support/users/captaincainer/)
 * (@captaincainer)
 * [7 years, 7 months ago](https://wordpress.org/support/topic/populate-select-menus-from-function/#post-10774263)
 * That’s great, thank you Alessandro.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Populate select menus from function’ is closed to new replies.

 * ![](https://ps.w.org/wp-user-manager/assets/icon-256x256.png?rev=3468506)
 * [WP User Manager - User Profile Builder & Membership](https://wordpress.org/plugins/wp-user-manager/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-user-manager/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-user-manager/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-user-manager/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-user-manager/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-user-manager/reviews/)

## Tags

 * [select option](https://wordpress.org/support/topic-tag/select-option/)

 * 4 replies
 * 2 participants
 * Last reply from: [captaincainer](https://wordpress.org/support/users/captaincainer/)
 * Last activity: [7 years, 7 months ago](https://wordpress.org/support/topic/populate-select-menus-from-function/#post-10774263)
 * Status: resolved