wp_list_users()
云策文档标注
概述
wp_list_users() 函数用于列出网站的所有用户,提供多种参数选项来控制输出格式和内容。它支持排序、分页、排除特定用户等功能,并可选择以 HTML 列表或纯文本形式返回结果。
关键要点
- 函数接受一个可选的 $args 参数数组,用于自定义用户列表的显示方式。
- 主要参数包括 orderby(排序字段)、order(排序方向)、number(返回用户数量)、exclude_admin(是否排除管理员账户)、show_fullname(是否显示全名)、echo(是否直接输出)等。
- 默认情况下,函数会直接输出用户列表;若设置 echo 为 false,则返回字符串供进一步处理。
- 通过 wp_list_users_args 过滤器可以修改查询参数,增强灵活性。
- 输出样式可通过 style 参数设置为 'list'(HTML 列表)或默认逗号分隔。
代码示例
// 直接输出用户列表
wp_list_users();
// 返回用户列表字符串,不直接输出
$users = wp_list_users( array( 'echo' => false ) );
echo $users;注意事项
- 使用 exclude 和 include 参数时,可以传入数组或逗号/空格分隔的字符串来排除或包含特定用户 ID。
- 当 html 参数设置为 false 时,输出为纯文本,忽略所有 HTML 格式。
- feed 和 feed_image 参数用于添加用户 feed 链接,需配合 feed_type 使用。
原文内容
Lists all the users of the site, with several options available.
Parameters
$argsstring|arrayoptional-
Array or string of default arguments.
orderbystringHow to sort the users. Accepts'nicename','email','url','registered','user_nicename','user_email','user_url','user_registered','name','display_name','post_count','ID','meta_value','user_login'. Default'name'.orderstringSorting direction for $orderby. Accepts'ASC','DESC'. Default'ASC'.numberintMaximum users to return or display. Default empty (all users).exclude_adminboolWhether to exclude the'admin'account, if it exists. Default false.show_fullnameboolWhether to show the user’s full name. Default false.feedstringIf not empty, show a link to the user’s feed and use this text as the alt parameter of the link.feed_imagestringIf not empty, show a link to the user’s feed and use this image URL as clickable anchor.feed_typestringThe feed type to link to, such as'rss2'. Defaults to default feed type.echoboolWhether to output the result or instead return it. Default true.stylestringIf'list', each user is wrapped in an<li>element, otherwise the users will be separated by commas.htmlboolWhether to list the items in HTML form or plaintext. Default true.excludestringAn array, comma-, or space-separated list of user IDs to exclude.includestringAn array, comma-, or space-separated list of user IDs to include.
Default:
array()
Source
function wp_list_users( $args = array() ) {
$defaults = array(
'orderby' => 'name',
'order' => 'ASC',
'number' => '',
'exclude_admin' => true,
'show_fullname' => false,
'feed' => '',
'feed_image' => '',
'feed_type' => '',
'echo' => true,
'style' => 'list',
'html' => true,
'exclude' => '',
'include' => '',
);
$parsed_args = wp_parse_args( $args, $defaults );
$return = '';
$query_args = wp_array_slice_assoc( $parsed_args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
$query_args['fields'] = 'ids';
/**
* Filters the query arguments for the list of all users of the site.
*
* @since 6.1.0
*
* @param array $query_args The query arguments for get_users().
* @param array $parsed_args The arguments passed to wp_list_users() combined with the defaults.
*/
$query_args = apply_filters( 'wp_list_users_args', $query_args, $parsed_args );
$users = get_users( $query_args );
foreach ( $users as $user_id ) {
$user = get_userdata( $user_id );
if ( $parsed_args['exclude_admin'] && 'admin' === $user->display_name ) {
continue;
}
if ( $parsed_args['show_fullname'] && '' !== $user->first_name && '' !== $user->last_name ) {
$name = sprintf(
/* translators: 1: User's first name, 2: Last name. */
_x( '%1$s %2$s', 'Display name based on first name and last name' ),
$user->first_name,
$user->last_name
);
} else {
$name = $user->display_name;
}
if ( ! $parsed_args['html'] ) {
$return .= $name . ', ';
continue; // No need to go further to process HTML.
}
if ( 'list' === $parsed_args['style'] ) {
$return .= '<li>';
}
$row = $name;
if ( ! empty( $parsed_args['feed_image'] ) || ! empty( $parsed_args['feed'] ) ) {
$row .= ' ';
if ( empty( $parsed_args['feed_image'] ) ) {
$row .= '(';
}
$row .= '<a href="' . get_author_feed_link( $user->ID, $parsed_args['feed_type'] ) . '"';
$alt = '';
if ( ! empty( $parsed_args['feed'] ) ) {
$alt = ' alt="' . esc_attr( $parsed_args['feed'] ) . '"';
$name = $parsed_args['feed'];
}
$row .= '>';
if ( ! empty( $parsed_args['feed_image'] ) ) {
$row .= '<img src="' . esc_url( $parsed_args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
} else {
$row .= $name;
}
$row .= '</a>';
if ( empty( $parsed_args['feed_image'] ) ) {
$row .= ')';
}
}
$return .= $row;
$return .= ( 'list' === $parsed_args['style'] ) ? '</li>' : ', ';
}
$return = rtrim( $return, ', ' );
if ( ! $parsed_args['echo'] ) {
return $return;
}
echo $return;
}
Hooks
- apply_filters( ‘wp_list_users_args’, array $query_args, array $parsed_args )
-
Filters the query arguments for the list of all users of the site.
Changelog
| Version | Description |
|---|---|
| 5.9.0 | Introduced. |
Skip to note 2 content
Rafa Carvalhido
Produces a list of users by user display_name.
or
$users = wp_list_users( array( 'echo' => false ) ); echo $users;Results: