wppaste
WordPress

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

Since
2.3.0, 4.5.0, 4.7.0, 5.9.0
Source
wp-includes/user.php:1640
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 to false. The 'include' and 'exclude' arguments are optional; if they are not specified, all users will be displayed. Only one can be used in a single call, either 'include' or 'exclude', but not both.

Compatibility

WordPress
since 5.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$argsarray|stringoptional
Array or string of arguments to generate a drop-down of users. See WP_User_Query::prepare_query() for additional available arguments.Default: ''
  • $show_option_allstringdefault: empty

    Text to show as the drop-down default (all).
  • $show_option_nonestringdefault: empty

    Text to show as the drop-down default when no users were found.
  • $option_none_valueint|stringdefault: -1

    Value to use for $show_option_none when no users were found.
  • $hide_if_only_one_authorstringdefault: empty

    Whether to skip generating the drop-down if only one user was found.
  • $orderbystringdefault: 'display_name'

    Field to order found users by. Accepts user fields.
  • $orderstringdefault: 'ASC'

    Whether to order users in ascending or descending order. Accepts 'ASC' (ascending) or 'DESC' (descending).
  • $includeint[]|stringdefault: empty

    Array or comma-separated list of user IDs to include.
  • $excludeint[]|stringdefault: empty

    Array or comma-separated list of user IDs to exclude.
  • $multibool|intdefault: 0|false

    Whether to skip the ID attribute on the 'select' element. Accepts 1|true or 0|false.
  • $showstringdefault: 'display_name'

    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.
  • $echoint|booldefault: 1|true

    Whether to echo or return the drop-down. Accepts 1|true (echo) or 0|false (return).
  • $selectedintdefault: 0

    Which user ID should be selected.
  • $include_selectedbooldefault: false

    Whether to always include the selected user ID in the drop- down.
  • $namestringdefault: 'user'

    Name attribute of select element.
  • $idstringdefault: is the value of $name

    ID attribute of the select element.
  • $classstringdefault: empty

    Class attribute of the select element.
  • $blog_idintdefault: is ID of the current blog

    ID of blog (Multisite only).
  • $whostringdefault: empty (all users)

    Deprecated, use $capability instead. Which type of users to query. Accepts only an empty string or 'authors'.
  • $rolestring|string[]default: empty

    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[]default: empty array

    An array of role names. Matched users must have at least one of these roles.
  • $role__not_instring[]default: empty array

    An array of role names to exclude. Users matching one or more of these roles will not be included in results.
  • $capabilitystring|string[]default: empty

    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[]default: empty array

    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[]default: empty array

    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'.

Return value

string
HTML dropdown list of users.

Performance profile

How much work a call to wp_dropdown_users() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_users().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
79–170

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 240.

Plugin surface
2 hooks

Third-party callbacks on 'wp_dropdown_users_args', 'wp_dropdown_users' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_users()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_dropdown_users() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_author()79–86get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), apply_filters()
!is_author() && !empty($users) && !$parsed_args110–137get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), esc_attr(), apply_filters()
!is_author() && !empty($users)117–142get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), esc_attr(), esc_attr(), apply_filters()
!is_author() && !empty($users)129–146get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), esc_attr(), get_userdata(), apply_filters()
!is_author() && !empty($users)129–156get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), esc_attr(), selected(), esc_attr(), apply_filters()
!is_author() && !empty($users)136–151get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), esc_attr(), esc_attr(), get_userdata(), apply_filters()
!is_author() && !empty($users) && $parsed_args136–161get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), esc_attr(), esc_attr(), selected(), esc_attr(), apply_filters()
!is_author() && !empty($users)148–165get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), esc_attr(), selected(), esc_attr(), get_userdata(), apply_filters()
!is_author() && !empty($users) && $parsed_args155–170get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), esc_attr(), esc_attr(), selected(), esc_attr(), get_userdata(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev23979–170231 fewer instruction than PHP 8.5
8.524079–17023
8.424079–17023
8.324079–17023
8.224079–17023
8.124079–170233 fewer instructions than PHP 7.4
7.424379–17223

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_dropdown_users() runs, in this order:

  1. apply_filters( wp_dropdown_users_args )filterline 1713 (+73 into the body)

    Filters the query arguments for the list of users in the dropdown.

  2. apply_filters( wp_dropdown_users )filterline 1779 (+139 into the body)

    Filters the wp_dropdown_users() HTML output.

Uses · 12

Used by · 2

Source code

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'] );

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.9.0
Added the 'capability', 'capabilityin', and 'capabilitynot_in' parameters.
Deprecated the 'who' parameter.from the docblock
4.7.0
Added the 'role', 'rolein', and 'rolenot_in' parameters.from the docblock
4.5.0
Added the 'display_name_with_login' value for 'show'.from the docblock
2.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.