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:1719
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: emptyText to show as the drop-down default (all).$show_option_nonestringdefault: emptyText to show as the drop-down default when no users were found.$option_none_valueint|stringdefault: -1Value to use for$show_option_nonewhen no users were found.$hide_if_only_one_authorstringdefault: emptyWhether 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: emptyArray or comma-separated list of user IDs to include.$excludeint[]|stringdefault: emptyArray or comma-separated list of user IDs to exclude.$multibool|intdefault: 0|falseWhether 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|trueWhether to echo or return the drop-down. Accepts 1|true (echo) or 0|false (return).$selectedintdefault: 0Which user ID should be selected.$include_selectedbooldefault: falseWhether to always include the selected user ID in the drop- down.$namestringdefault: 'user'Name attribute of select element.$idstringdefault: is the value of $nameID attribute of the select element.$classstringdefault: emptyClass attribute of the select element.$blog_idintdefault: is ID of the current blogID of blog (Multisite only).$whostringdefault: empty (all users)Deprecated, use$capabilityinstead. Which type of users to query. Accepts only an empty string or 'authors'.$rolestring|string[]default: emptyAn 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 arrayAn array of role names. Matched users must have at least one of these roles.$role__not_instring[]default: empty arrayAn array of role names to exclude. Users matching one or more of these roles will not be included in results.$capabilitystring|string[]default: emptyAn 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 arrayAn 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 arrayAn 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
- Scaling
- Scales with input
- Instructions
- 79–170
- Plugin surface
- 2 hooks
- Called by
- 2
Reaches the database via get_users().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 240.
Third-party callbacks on 'wp_dropdown_users_args', 'wp_dropdown_users' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
!is_author() | 79–86 | get_current_blog_id(), is_author(), wp_parse_args(), wp_array_slice_assoc(), apply_filters(), get_users(), apply_filters() |
!is_author() && !empty($users) && !$parsed_args | 110–137 | get_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–142 | get_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–146 | get_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–156 | get_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–151 | get_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_args | 136–161 | get_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–165 | get_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_args | 155–170 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 239 | 79–170 | 23 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 240 | 79–170 | 23 | |
| 8.4 | 240 | 79–170 | 23 | |
| 8.3 | 240 | 79–170 | 23 | |
| 8.2 | 240 | 79–170 | 23 | |
| 8.1 | 240 | 79–170 | 23 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 243 | 79–172 | 23 |
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:
- apply_filters( wp_dropdown_users_args )filterline 1792 (+73 into the body)
Filters the query arguments for the list of users in the dropdown.
- apply_filters( wp_dropdown_users )filterline 1858 (+139 into the body)
Filters the wp_dropdown_users() HTML output.
Uses · 12
- get_current_blog_id()Retrieves the current site ID.
- is_author()Determines whether the query is for an existing author archive page.
- get_query_var()Retrieves the value of a query variable in the WP_Query class.
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_array_slice_assoc()Extracts a slice of an array, given a list of keys.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_users()Retrieves list of users matching criteria.
- esc_attr()Escaping for HTML attributes.
- selected()Outputs the HTML selected attribute.
- get_userdata()Retrieves user info by user ID.
- _x()Retrieves translated string with gettext context.
- esc_html()Escaping for HTML blocks.
Used by · 2
- WP_Posts_List_Table::inline_edit()Outputs the hidden row displayed when inline editing
- post_author_meta_box()Displays form field with list of authors.
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.
Signature, return type and hooks compared across 5 parsed releases.
Deprecated the 'who' parameter.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.