wp_dropdown_users_args
云策文档标注
概述
wp_dropdown_users_args 是一个 WordPress 过滤器,用于修改 wp_dropdown_users() 函数中用户下拉列表的查询参数。它允许开发者自定义用户查询条件,如角色、权限、排序等,以控制下拉列表中显示的用户。
关键要点
- 过滤器名称:wp_dropdown_users_args,用于过滤 wp_dropdown_users() 的查询参数。
- 参数:接受两个参数,$query_args(传递给 get_users() 的查询参数数组)和 $parsed_args(wp_dropdown_users() 的合并参数数组)。
- 核心功能:通过修改 $query_args 数组中的键值(如 role、role__in、role__not_in、capability、orderby 等)来筛选和排序用户。
- 应用场景:常用于后台文章编辑器的作者下拉列表,但也可用于其他用户下拉场景。
- 注意事项:在 Gutenberg 编辑器中,此过滤器可能不生效,需使用 rest_user_query 过滤器替代。
代码示例
// 示例:添加订阅者到下拉列表
add_filter( 'wp_dropdown_users_args', 'wpdocs_add_subscribers_to_dropdown' );
function wpdocs_add_subscribers_to_dropdown( $query_args ) {
$query_args['who'] = '';
return $query_args;
}注意事项
- 参数 who 已弃用,建议使用 role 或 capability 参数进行用户筛选。
- capability 参数仅适用于数据库中存在或通过 map_meta_cap 过滤的权限。
- 在修改查询参数时,需注意默认值(如 who 默认为 'authors'),可能需要取消设置以避免冲突。
原文内容
Filters the query arguments for the list of users in the dropdown.
Parameters
$query_argsarray-
The query arguments for get_users() .
More Arguments from get_users( … $args )
Arguments to retrieve users. See WP_User_Query::prepare_query() for more information on accepted arguments.
$parsed_argsarray-
The arguments passed to wp_dropdown_users() combined with the defaults.
More Arguments from wp_dropdown_users( … $args )
Array or string of arguments to generate a drop-down of users.
See WP_User_Query::prepare_query() for additional available arguments.show_option_allstringText to show as the drop-down default (all).
Default empty.show_option_nonestringText to show as the drop-down default when no users were found. Default empty.option_none_valueint|stringValue to use for$show_option_nonewhen no users were found. Default -1.hide_if_only_one_authorstringWhether to skip generating the drop-down if only one user was found. Default empty.orderbystringField to order found users by. Accepts user fields.
Default'display_name'.orderstringWhether to order users in ascending or descending order. Accepts'ASC'(ascending) or'DESC'(descending).
Default'ASC'.includeint[]|stringArray or comma-separated list of user IDs to include.
Default empty.excludeint[]|stringArray or comma-separated list of user IDs to exclude.
Default empty.multibool|intWhether to skip the ID attribute on the'select'element.
Accepts1|trueor0|false. Default0|false.showstringUser 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|boolWhether to echo or return the drop-down. Accepts1|true(echo) or0|false(return). Default1|true.selectedintWhich user ID should be selected. Default 0.include_selectedboolWhether to always include the selected user ID in the drop- down. Default false.namestringName attribute of select element. Default'user'.idstringID attribute of the select element. Default is the value of$name.classstringClass attribute of the select element. Default empty.blog_idintID of blog (Multisite only). Default is ID of the current blog.whostringDeprecated, use$capabilityinstead.
Which type of users to query. Accepts only an empty string or'authors'. Default empty (all users).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. Default empty.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.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’. Default empty.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’. Default empty array.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’. Default empty array.
Source
$query_args = apply_filters( 'wp_dropdown_users_args', $query_args, $parsed_args );
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |
Skip to note 6 content
Jonathan Goldford
This filter allows you to set subscribers as authors of blog posts within the admin without giving them additional privileges. Here’s the code:
add_filter( 'wp_dropdown_users_args', 'wpdocs_add_subscribers_to_dropdown' ); function wpdocs_add_subscribers_to_dropdown( $query_args ) { $query_args['who'] = ''; return $query_args; }Skip to note 7 content
RiseOfLex88
In addition to Jonathan’s contribution, you can also expand it further by specifying the role. This is undocumented on wp codex. But does work e.g.
function wpdocs_add_subscribers_to_dropdown( $query_args, $r ) { $query_args['role'] = array('contributor'); // Unset the 'who' as this defaults to the 'author' role unset( $query_args['who'] ); return $query_args; } add_filter( 'wp_dropdown_users_args', 'wpdocs_add_subscribers_to_dropdown', 10, 2 );Skip to note 8 content
pgDarkness
In addition to RiseOfLex88’s contribution, you can also specify multiple roles.
function wpdocs_add_subscribers_to_dropdown( $query_args ) { // Use this array to specify multiple roles to show in dropdown $query_args['role__in'] = array( 'contributor', 'administrator' ); // Use this array to specify multiple roles to hide in dropdown $query_args['role__not_in'] = array( 'editor' ); // Unset the 'who' as this defaults to the 'author' role unset( $query_args['who'] ); return $query_args; } add_filter( 'wp_dropdown_users_args', 'wpdocs_add_subscribers_to_dropdown' );If you dump variable “$query_args” inside this function, this is what you will get:
array(10) { ["blog_id"]=> int(1) ["include"]=> string(0) "" ["exclude"]=> string(0) "" ["orderby"]=> string(12) "display_name" ["order"]=> string(3) "ASC" ["who"]=> string(7) "authors" ["role"]=> string(0) "" ["role__in"]=> array(0) { } ["role__not_in"]=> array(0) { } ["fields"]=> array(3) { [0]=> string(2) "ID" [1]=> string(10) "user_login" [2]=> string(12) "display_name" } }Skip to note 9 content
Sarah Lewis
Note that if you’re trying to filter the “Author” dropdown on the post editor screen, and the post editor is using Gutenberg, this filter doesn’t fire. You’ll need to work with the rest_user_query filter instead. (But also note that this filter is used for a lot more than the Author dropdown, so use it thoughtfully.)
Skip to note 10 content
JamesKoussertari
For this to work with Subscribers, you’ll also need to adjust the capability:
// Show Subscribers in the 'author' drop down on the classic post editor function wpdocs_add_subscribers_to_dropdown( $query_args ) { $query_args['who'] = ''; // reset the query $query_args['capability'] = ''; // reset the query $query_args['role__in'] = array( 'administrator', 'subscriber', 'author', 'editor' ); $query_args['capability__in'] = array( 'edit_own_posts' ); // Custom capability for subscribers return $query_args; } add_filter( 'wp_dropdown_users_args', 'wpdocs_add_subscribers_to_dropdown' );