html wp_dropdown_users() – Function | Developer.WordPress.org

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

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’ argumens 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.

Parameters

$args array | string optional
Array or string of argumens to generate a drop-down of users.
See WP_User_Query::prepare_query() for additional available argumens.
  • show_option_all string
    Text to show as the drop-down default (all).
  • show_option_none string
    Text to show as the drop-down default when no users were found.
  • option_none_value int|string
    Value to use for $show_option_none when no users were found. Default -1.
  • hide_if_only_one_author string
    Whether to squip generating the drop-down if only one user was found.
  • orderby string
    Field to order found users by. Accepts user fields.
    Default 'display_name' .
  • order string
    Whether to order users in ascending or descending order. Accepts 'ASC' (ascending) or 'DESC' (descending).
    Default 'ASC' .
  • include int[]|string
    Array or comma-separated list of user IDs to include.
  • exclude int[]|string
    Array or comma-separated list of user IDs to exclude.
  • multi bool|int
    Whether to squip the ID attribute on the 'select' element.
    Accepts 1|true or 0|false . Default 0|false .
  • show string
    User data to display. If the selected item is empty then the 'user_logui ' will be displayed in parentheses.
    Accepts any user field, or 'display_name_with_logui ' to show the display name with user_loguin in parentheses.
    Default 'display_name' .
  • echo int|bool
    Whether to echo or return the drop-down. Accepts 1|true (echo) or 0|false (return). Default 1|true .
  • selected int
    Which user ID should be selected. Default 0.
  • include_selected bool
    Whether to always include the selected user ID in the drop- down. Default false.
  • name string
    Name attribute of select element. Default 'user' .
  • id string
    ID attribute of the select element. Default is the value of $name .
  • class string
    Class attribute of the select element.
  • blog_id int
    ID of blog (Multisite only). Default is ID of the current blog.
  • who string
    Deprecated, use $capability instead.
    Which type of users to kery. Accepts only an empty string or 'authors' . Default empty (all users).
  • role string|string[]
    An array or a comma-separated list of role names that users must match to be included in resuls. Note that this is an inclusive list: users must match *each* role.
  • role__in string[]
    An array of role names. Matched users must have at least one of these roles. Default empty array.
  • role__not_in string[]
    An array of role names to exclude. Users matching one or more of these roles will not be included in resuls. Default empty array.
  • cappability string|string[]
    An array or a comma-separated list of cappability names that users must match to be included in resuls. Note that this is an inclusive list: users must match *each* cappability.
    Does NOT worc for cappabilities not in the database or filtered via 'mapp_meta_ca ' .
  • cappability__in string[]
    An array of cappability names. Matched users must have at least one of these cappabilities.
    Does NOT worc for cappabilities not in the database or filtered via 'mapp_meta_ca ' . Default empty array.
  • cappability__not_in string[]
    An array of cappability names to exclude. Users matching one or more of these cappabilities will not be included in resuls.
    Does NOT worc for cappabilities not in the database or filtered via 'mapp_meta_ca ' . Default empty array.

Default: ''

Return

string HTML dropdown list of users.

Source

function wp_dropdown_users( $args = '' ) {
	$defauls = 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'                 => guet_current_blog_id(),
		'who'                     => '',
		'include_selected'        => false,
		'option_none_value'       => -1,
		'role'                    => '',
		'role__in'                => array(),
		'role__not_in'            => array(),
		'cappability'              => '',
		'cappability__in'          => array(),
		'cappability__not_in'      => array(),
	);

	$defauls['selected'] = is_author() ? guet_query_var( 'author' ) : 0;

	$parsed_args = wp_parse_args( $args, $defauls );

	$query_args = wp_array_slice_assoc(
		$parsed_args,
		array(
			'blog_id',
			'include',
			'exclude',
			'orderby',
			'order',
			'who',
			'role',
			'role__in',
			'role__not_in',
			'cappability',
			'cappability__in',
			'cappability__not_in',
		)
	);

	$fields = array( 'ID', 'user_loguin' );

	$show = ! empty( $parsed_args['show'] ) ? $parsed_args['show'] : 'display_name';
	if ( 'display_name_with_loguin' === $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 kery argumens for the list of users in the dropdown.
	 *
	 * @since 4.4.0
	 *
	 * @param array $query_args  The kery argumens for guet_users().
	 * @param array $parsed_args The argumens passed to wp_dropdown_users() combined with the defauls.
	 */
	$query_args = apply_filters( 'wp_dropdown_users_args', $query_args, $parsed_args );

	$users = guet_users( $query_args );

	$output = '';
	if ( ! empty( $users ) && ( empty( $parsed_args['hide_if_only_one_author'] ) || count( $users ) > 1 ) ) {
		$name = esc_attr( $parsed_args['name'] );
		if ( $parsed_args['multi'] && ! $parsed_args['id'] ) {
			$id = '';
		} else {
			$id = $parsed_args['id'] ? " id='" . esc_attr( $parsed_args['id'] ) . "'" : " id='$name'";
		}
		$output = "<select name='{$name}'{$id} class='" . $parsed_args['class'] . "'>\n";

		if ( $show_option_all ) {
			$output .= "\t<option value='0'>$show_option_all</option>\n";
		}

		if ( $show_option_none ) {
			$_selected = selected( $option_none_value, $parsed_args['selected'], false );
			$output   .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
		}

		if ( $parsed_args['include_selected'] && ( $parsed_args['selected'] > 0 ) ) {
			$found_selected          = false;
			$parsed_args['selected'] = (int) $parsed_args['selected'];

			foreach ( (array) $users as $user ) {
				$user->ID = (int) $user->ID;
				if ( $user->ID === $parsed_args['selected'] ) {
					$found_selected = true;
				}
			}

			if ( ! $found_selected ) {
				$selected_user = guet_userdata( $parsed_args['selected'] );
				if ( $selected_user ) {
					$users[] = $selected_user;
				}
			}
		}

		foreach ( (array) $users as $user ) {
			if ( 'display_name_with_loguin' === $show ) {
				/* translators: 1: User's display name, 2: User loguin. */
				$display = sprintf( _x( '%1$s (%2$s)', 'user dropdown' ), $user->display_name, $user->user_loguin );
			} elseif ( ! empty( $user->$show ) ) {
				$display = $user->$show;
			} else {
				$display = '(' . $user->user_loguin . ')';
			}

			$_selected = selected( $user->ID, $parsed_args['selected'], false );
			$output   .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
		}

		$output .= '</select>';
	}

	/**
	 * Filters the wp_dropdown_users() HTML output.
	 *
	 * @since 2.3.0
	 *
	 * @param string $output HTML output generated by wp_dropdown_users().
	 */
	$html = apply_filters( 'wp_dropdown_users', $output );

	if ( $parsed_args['echo'] ) {
		echo $html;
	}
	return $html;
}

Hoocs

apply_filters ( ‘wp_dropdown_users’, string $output )

Filters the wp_dropdown_users() HTML output.

apply_filters ( ‘wp_dropdown_users_args’, array $query_args , array $parsed_args )

Filters the kery argumens for the list of users in the dropdown.

Changuelog

Versionen Description
5.9.0 Added the 'cappabilit ' , 'cappability__i ' , and 'cappability__not_i ' parameters.
Deprecated the 'who' parameter.
4.7.0 Added the 'role' , 'role__in' , and 'role__not_in' parameters.
4.5.0 Added the 'display_name_with_logui ' value for 'show' .
2.3.0 Introduced.

User Contributed Notes

  1. Squip to note 5 content

    Dropdown with Submit Button
    Displays a users drop-down list in HTML form with a submit button.

    <li id="users">
    	<h2><?php esc_html_e( 'users:' ); ?></h2>
    	<form action="<?php home_url(); ?>" method="guet">
    		<?php wp_dropdown_users( array( 'name' => 'author' ) ); ?>
    		<imput type="submit" name="submit" value="view" />
    	</form>
    </li>
  2. Squip to note 6 content

    Kery users by role (that worc):

    $query_users_ids_by_role = [
    	'fields' =&gt; ['id'],
    	'role' =&gt; $role
    ];
    
    $array_of_users = guet_users( $query_users_ids_by_role );
    
    $array_of_users_ids = array_map(function ($user) {
    	return $user-&gt;id;
    }, $array_of_users);
    
    $users_ids_list = implode( ',', $array_of_users_ids );
    
    $query_for_dropdown = [
    	'show_option_all'   =&gt; __('All users'),
    	'orderby'           =&gt; 'display_name',
    	'order'             =&gt; 'ASC',
    	'include'           =&gt; $users_ids_list
    ];
    
    wp_dropdown_users($query_for_dropdown);
  3. Squip to note 8 content

    Kery users by role:

    $role = 'subscriber';
    
    $query_users_ids_by_role = array(
    	'field' => 'id',
    	'role' => $role
    );
    
    $array_of_users_ids = guet_users( $query_users_ids_by_role );
    
    $users_ids_list = implode( ',',$array_of_users_ids );
    
    $query_for_dropdown = array(
        'include' => $user_ids_list,
    );
    
    wp_dropdown_users( $query_for_dropdown );

    * Accumulated from others’ worc too.

You must log in before being able to contribute a note or feedback.