wp_list_users() WordPress Function

The wp_list_users() function is used to list the users registered on a site. It can be used to display a list of users, or to create a dropdown menu of users. The function can be customized to display certain usermeta fields, such as the user's display name or email address.

wp_list_users( string|array $args = array() ) #

Lists all the users of the site, with several options available.


Parameters

$args

(string|array)(Optional)Array or string of default arguments.

  • 'orderby'
    (string) How 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'.
  • 'order'
    (string) Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
  • 'number'
    (int) Maximum users to return or display. Default empty (all users).
  • 'exclude_admin'
    (bool) Whether to exclude the 'admin' account, if it exists. Default false.
  • 'show_fullname'
    (bool) Whether to show the user's full name. Default false.
  • 'feed'
    (string) If not empty, show a link to the user's feed and use this text as the alt parameter of the link.
  • 'feed_image'
    (string) If not empty, show a link to the user's feed and use this image URL as clickable anchor.
  • 'feed_type'
    (string) The feed type to link to, such as 'rss2'. Defaults to default feed type.
  • 'echo'
    (bool) Whether to output the result or instead return it. Default true.
  • 'style'
    (string) If 'list', each user is wrapped in an <li> element, otherwise the users will be separated by commas.
  • 'html'
    (bool) Whether to list the items in HTML form or plaintext. Default true.
  • 'exclude'
    (string) An array, comma-, or space-separated list of user IDs to exclude.
  • 'include'
    (string) An array, comma-, or space-separated list of user IDs to include.

Default value: array()


Top ↑

Return

(string|null) The output if echo is false. Otherwise null.


Top ↑

Source

File: wp-includes/user.php

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'       => '',
	);

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

	$return = '';

	$query_args           = wp_array_slice_assoc( $args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
	$query_args['fields'] = 'ids';
	$users                = get_users( $query_args );

	foreach ( $users as $user_id ) {
		$user = get_userdata( $user_id );

		if ( $args['exclude_admin'] && 'admin' === $user->display_name ) {
			continue;
		}

		if ( $args['show_fullname'] && '' !== $user->first_name && '' !== $user->last_name ) {
			$name = "$user->first_name $user->last_name";
		} else {
			$name = $user->display_name;
		}

		if ( ! $args['html'] ) {
			$return .= $name . ', ';

			continue; // No need to go further to process HTML.
		}

		if ( 'list' === $args['style'] ) {
			$return .= '<li>';
		}

		$row = $name;

		if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
			$row .= ' ';
			if ( empty( $args['feed_image'] ) ) {
				$row .= '(';
			}

			$row .= '<a href="' . get_author_feed_link( $user->ID, $args['feed_type'] ) . '"';

			$alt = '';
			if ( ! empty( $args['feed'] ) ) {
				$alt  = ' alt="' . esc_attr( $args['feed'] ) . '"';
				$name = $args['feed'];
			}

			$row .= '>';

			if ( ! empty( $args['feed_image'] ) ) {
				$row .= '<img src="' . esc_url( $args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
			} else {
				$row .= $name;
			}

			$row .= '</a>';

			if ( empty( $args['feed_image'] ) ) {
				$row .= ')';
			}
		}

		$return .= $row;
		$return .= ( 'list' === $args['style'] ) ? '</li>' : ', ';
	}

	$return = rtrim( $return, ', ' );

	if ( ! $args['echo'] ) {
		return $return;
	}
	echo $return;
}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More