This tutorial explains how to add a dropdown/select menu of all users registered with the site.

Goal

The goal of this tutorial is to display a dropdown menu listing all registered users. This will enable the person who is submitting the form to select the user from a predefined list, so they don’t have to know in advance the names of all users.

Step 1: Add the function

First, open your theme’s functions.php file and add the following code:

// shortcode to display menu of existing users
function usp_existing_users_menu() {
	
	$content  = '<fieldset class="usp-fieldset">';
	$content .= '<label for="usp-custom-1" class="usp-label">Select User</label>';
	$content .= '<select name="usp-custom-1" data-required="true" class="usp-input" required="required">';
	$content .= '<option value="" selected="selected">Please Select...</option>';
	
	$users = get_users();
	foreach ($users as $user) {
		$content .= '<option value="'. esc_html($user->user_login) .'">'. esc_html($user->user_login) .'</option>';
	}
	
	$content .= '</select>';
	$content .= '<input name="usp-custom-1-required" value="1" type="hidden" />';
	$content .= '</fieldset>';
	
	return $content;
}
add_shortcode('usp_users', 'usp_existing_users_menu');

This code creates a shortcode named [usp_users] that can be included in any form. When included, it displays a dropdown menu of all registered users.

Step 2: Add the shortcode

Next, add the following shortcode in your USP Form:

[usp_users]

With that in place, viewing the form on the front-end will reveal the user menu ready to go. If you try submitting a test post, it will have the selected username attached as a Custom Field named usp-custom-1. You can verify the Custom Field by examining the “Custom Fields” panel, which is located beneath the post-content field on the “Edit Post” screen.

And done. There are many ways to customize and implement this technique, depending on your needs. This quick tutorial is just to get you started in the right direction.

Happy form building! :)