wp_dropdown_users( array|string $args = '' ): string

Creates dropdown HTML content of users.


Description

The content can either be displayed, which it is by default or retrieved by setting the ‘echo’ argument. The ‘include’ and ‘exclude’ arguments do not need to be used; all users will be displayed in that case. Only one can be used, either ‘include’ or ‘exclude’, but not both.

The available arguments are as follows:


Top ↑

Parameters

$args array|string Optional
Array or string of arguments to generate a drop-down of users.
See WP_User_Query::prepare_query() for additional available arguments.
  • show_option_allstring
    Text to show as the drop-down default (all).
  • show_option_nonestring
    Text to show as the drop-down default when no users were found.
  • option_none_valueint|string
    Value to use for $show_option_non when no users were found. Default -1.
  • hide_if_only_one_authorstring
    Whether to skip generating the drop-down if only one user was found.
  • orderbystring
    Field to order found users by. Accepts user fields.
    Default 'display_name'.
  • orderstring
    Whether to order users in ascending or descending order. Accepts 'ASC' (ascending) or 'DESC' (descending).
    Default 'ASC'.
  • includeint[]|string
    Array or comma-separated list of user IDs to include.
  • excludeint[]|string
    Array or comma-separated list of user IDs to exclude.
  • multibool|int
    Whether to skip the ID attribute on the 'select' element.
    Accepts 1|true or 0|false. Default 0|false.
  • showstring
    User data to display. If the selected item is empty then the 'user_login' will be displayed in parentheses.
    Accepts any user field, or 'display_name_with_login' to show the display name with user_login in parentheses.
    Default 'display_name'.
  • echoint|bool
    Whether to echo or return the drop-down. Accepts 1|true (echo) or 0|false (return). Default 1|true.
  • selectedint
    Which user ID should be selected. Default 0.
  • include_selectedbool
    Whether to always include the selected user ID in the drop- down. Default false.
  • namestring
    Name attribute of select element. Default 'user'.
  • idstring
    ID attribute of the select element. Default is the value of $name.
  • classstring
    Class attribute of the select element.
  • blog_idint
    ID of blog (Multisite only). Default is ID of the current blog.
  • whostring
    Which type of users to query. Accepts only an empty string or 'authors'.
  • rolestring|array
    An array or a comma-separated list of role names that users must match to be included in results. Note that this is an inclusive list: users must match *each* role.
  • role__instring[]
    An array of role names. Matched users must have at least one of these roles. Default empty array.
  • role__not_instring[]
    An array of role names to exclude. Users matching one or more of these roles will not be included in results. Default empty array.
More Arguments from WP_User_Query::prepare_query( ... $query ) Array or string of Query parameters.
  • blog_idint
    The site ID. Default is the current site.
  • rolestring|string[]
    An array or a comma-separated list of role names that users must match to be included in results. Note that this is an inclusive list: users must match *each* role.
  • role__instring[]
    An array of role names. Matched users must have at least one of these roles.
  • role__not_instring[]
    An array of role names to exclude. Users matching one or more of these roles will not be included in results.
  • meta_keystring|string[]
    Meta key or keys to filter by.
  • meta_valuestring|string[]
    Meta value or values to filter by.
  • meta_comparestring
    MySQL operator used for comparing the meta value.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_compare_keystring
    MySQL operator used for comparing the meta key.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_typestring
    MySQL data type that the meta_value column will be CAST to for comparisons.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_type_keystring
    MySQL data type that the meta_key column will be CAST to for comparisons.
    See WP_Meta_Query::__construct() for accepted values and default value.
  • meta_queryarray
    An associative array of WP_Meta_Query arguments.
    See WP_Meta_Query::__construct() for accepted values.
  • capabilitystring|string[]
    An array or a comma-separated list of capability names that users must match to be included in results. Note that this is an inclusive list: users must match *each* capability.
    Does NOT work for capabilities not in the database or filtered via 'map_meta_cap'.
  • capability__instring[]
    An array of capability names. Matched users must have at least one of these capabilities.
    Does NOT work for capabilities not in the database or filtered via 'map_meta_cap'.
  • capability__not_instring[]
    An array of capability names to exclude. Users matching one or more of these capabilities will not be included in results.
    Does NOT work for capabilities not in the database or filtered via 'map_meta_cap'.
  • includeint[]
    An array of user IDs to include.
  • excludeint[]
    An array of user IDs to exclude.
  • searchstring
    Search keyword. Searches for possible string matches on columns.
    When $search_columns is left empty, it tries to determine which column to search in based on search string.
  • search_columnsstring[]
    Array of column names to be searched. Accepts 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename', 'display_name'.
  • orderbystring|array
    Field(s) to sort the retrieved users by. May be a single value, an array of values, or a multi-dimensional array with fields as keys and orders ('ASC' or 'DESC') as values. Accepted values are:
    • 'ID'
    • 'display_name' (or 'name')
    • 'include'
    • 'user_login' (or 'login')
    • 'login__in'
    • 'user_nicename' (or 'nicename'),
    • 'nicename__in'
    • 'user_email (or 'email')
    • 'user_url' (or 'url'),
    • 'user_registered' (or 'registered')
    • 'post_count'
    • 'meta_value',
    • 'meta_value_num'
    • The value of $meta_key
    • An array key of $meta_query To use 'meta_value' or 'meta_value_num', $meta_key must be also be defined. Default 'user_login'.
  • orderstring
    Designates ascending or descending order of users. Order values passed as part of an $orderby array take precedence over this parameter. Accepts 'ASC', 'DESC'. Default 'ASC'.
  • offsetint
    Number of users to offset in retrieved results. Can be used in conjunction with pagination. Default 0.
  • numberint
    Number of users to limit the query for. Can be used in conjunction with pagination. Value -1 (all) is supported, but should be used with caution on larger sites.
    Default -1 (all users).
  • pagedint
    When used with number, defines the page of results to return.
    Default 1.
  • count_totalbool
    Whether to count the total number of users found. If pagination is not needed, setting this to false can improve performance.
    Default true.
  • fieldsstring|string[]
    Which fields to return. Single or all fields (string), or array of fields. Accepts:
    • 'ID'
    • 'display_name'
    • 'user_login'
    • 'user_nicename'
    • 'user_email'
    • 'user_url'
    • 'user_registered'
    • 'user_pass'
    • 'user_activation_key'
    • 'user_status'
    • 'spam' (only available on multisite installs)
    • 'deleted' (only available on multisite installs)
    • 'all' for all fields
    • 'all_with_meta' to include meta fields.
    Default 'all'.
  • whostring
    Type of users to query. Accepts 'authors'.
    Default empty (all users).
  • has_published_postsbool|string[]
    Pass an array of post types to filter results to users who have published posts in those post types. true is an alias for all public post types.
  • nicenamestring
    The user nicename.
  • nicename__instring[]
    An array of nicenames to include. Users matching one of these nicenames will be included in results.
  • nicename__not_instring[]
    An array of nicenames to exclude. Users matching one of these nicenames will not be included in results.
  • loginstring
    The user login.
  • login__instring[]
    An array of logins to include. Users matching one of these logins will be included in results.
  • login__not_instring[]
    An array of logins to exclude. Users matching one of these logins will not be included in results.

Default: ''


Top ↑

Return

string HTML dropdown list of users.


Top ↑

Source

File: wp-includes/user.php. View all references

function wp_dropdown_users( $args = '' ) {
	$defaults = array(
		'show_option_all'         => '',
		'show_option_none'        => '',
		'hide_if_only_one_author' => '',
		'orderby'                 => 'display_name',
		'order'                   => 'ASC',
		'include'                 => '',
		'exclude'                 => '',
		'multi'                   => 0,
		'show'                    => 'display_name',
		'echo'                    => 1,
		'selected'                => 0,
		'name'                    => 'user',
		'class'                   => '',
		'id'                      => '',
		'blog_id'                 => get_current_blog_id(),
		'who'                     => '',
		'include_selected'        => false,
		'option_none_value'       => -1,
		'role'                    => '',
		'role__in'                => array(),
		'role__not_in'            => array(),
		'capability'              => '',
		'capability__in'          => array(),
		'capability__not_in'      => array(),
	);

	$defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;

	$parsed_args = wp_parse_args( $args, $defaults );

	$query_args = wp_array_slice_assoc(
		$parsed_args,
		array(
			'blog_id',
			'include',
			'exclude',
			'orderby',
			'order',
			'who',
			'role',
			'role__in',
			'role__not_in',
			'capability',
			'capability__in',
			'capability__not_in',
		)
	);

	$fields = array( 'ID', 'user_login' );

	$show = ! empty( $parsed_args['show'] ) ? $parsed_args['show'] : 'display_name';
	if ( 'display_name_with_login' === $show ) {
		$fields[] = 'display_name';
	} else {
		$fields[] = $show;
	}

	$query_args['fields'] = $fields;

	$show_option_all   = $parsed_args['show_option_all'];
	$show_option_none  = $parsed_args['show_option_none'];
	$option_none_value = $parsed_args['option_none_value'];

	/**
	 * Filters the query arguments for the list of users in the dropdown.
	 *
	 * @since 4.4.0
	 *
	 * @param array $query_args  The query arguments for get_users().
	 * @param array $parsed_args The arguments passed to wp_dropdown_users() combined with the defaults.
	 */
	$query_args = apply_filters( 'wp_dropdown_users_args', $query_args, $parsed_args );

	$users = get_users( $query_args );

	$output = '';
	if ( ! empty( $users ) && ( empty( $parsed_args['hide_if_only_one_author'] ) || count( $users ) > 1 ) ) {
		$name = esc_attr( $parsed_args['name'] );
		if ( $parsed_args['multi'] && ! $parsed_args['id'] ) {
			$id = '';
		} else {
			$id = $parsed_args['id'] ? " id='" . esc_attr( $parsed_args['id'] ) . "'" : " id='$name'";
		}
		$output = "<select name='{$name}'{$id} class='" . $parsed_args['class'] . "'>\n";

		if ( $show_option_all ) {
			$output .= "\t<option value='0'>$show_option_all</option>\n";
		}

		if ( $show_option_none ) {
			$_selected = selected( $option_none_value, $parsed_args['selected'], false );
			$output   .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
		}

		if ( $parsed_args['include_selected'] && ( $parsed_args['selected'] > 0 ) ) {
			$found_selected          = false;
			$parsed_args['selected'] = (int) $parsed_args['selected'];

			foreach ( (array) $users as $user ) {
				$user->ID = (int) $user->ID;
				if ( $user->ID === $parsed_args['selected'] ) {
					$found_selected = true;
				}
			}

			if ( ! $found_selected ) {
				$selected_user = get_userdata( $parsed_args['selected'] );
				if ( $selected_user ) {
					$users[] = $selected_user;
				}
			}
		}

		foreach ( (array) $users as $user ) {
			if ( 'display_name_with_login' === $show ) {
				/* translators: 1: User's display name, 2: User login. */
				$display = sprintf( _x( '%1$s (%2$s)', 'user dropdown' ), $user->display_name, $user->user_login );
			} elseif ( ! empty( $user->$show ) ) {
				$display = $user->$show;
			} else {
				$display = '(' . $user->user_login . ')';
			}

			$_selected = selected( $user->ID, $parsed_args['selected'], false );
			$output   .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
		}

		$output .= '</select>';
	}

	/**
	 * Filters the wp_dropdown_users() HTML output.
	 *
	 * @since 2.3.0
	 *
	 * @param string $output HTML output generated by wp_dropdown_users().
	 */
	$html = apply_filters( 'wp_dropdown_users', $output );

	if ( $parsed_args['echo'] ) {
		echo $html;
	}
	return $html;
}

Top ↑

Hooks



Top ↑

Changelog

Changelog
Version Description
4.7.0 Added the $role, $role__in, and $role__not_in parameters.
4.5.0 Added the 'display_name_with_login' value for 'show'.
2.3.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Dropdown with Submit Button
    Displays a users drop-down list in HTML form with a submit button.

    <li id="users">
    	<h2><?php esc_html_e( 'users:' ); ?></h2>
    	<form action="<?php home_url(); ?>" method="get">
    		<?php wp_dropdown_users( array( 'name' => 'author' ) ); ?>
    		<input type="submit" name="submit" value="view" />
    	</form>
    </li>
  2. Skip to note 2 content
    Contributed by minimamente

    Query users by role (that work):

    $query_users_ids_by_role = [
    	'fields' =&gt; ['id'],
    	'role' =&gt; $role
    ];
    
    $array_of_users = get_users( $query_users_ids_by_role );
    
    $array_of_users_ids = array_map(function ($user) {
    	return $user-&gt;id;
    }, $array_of_users);
    
    $users_ids_list = implode( ',', $array_of_users_ids );
    
    $query_for_dropdown = [
    	'show_option_all'   =&gt; __('All users'),
    	'orderby'           =&gt; 'display_name',
    	'order'             =&gt; 'ASC',
    	'include'           =&gt; $users_ids_list
    ];
    
    wp_dropdown_users($query_for_dropdown);
  3. Skip to note 4 content
    Contributed by Mike Ritter

    Query users by role:

    $role = 'subscriber';
    
    $query_users_ids_by_role = array(
    	'field' => 'id',
    	'role' => $role
    );
    
    $array_of_users_ids = get_users( $query_users_ids_by_role );
    
    $users_ids_list = implode( ',',$array_of_users_ids );
    
    $query_for_dropdown = array(
        'include' => $user_ids_list,
    );
    
    wp_dropdown_users( $query_for_dropdown );

    * Accumulated from others’ work too.

You must log in before being able to contribute a note or feedback.