wp_xmlrpc_server::wp_getUsers() WordPress Method

The wp_xmlrpc_server::wp_getUsers() method is used to get a list of users from a WordPress site. This is useful for managing multiple WordPress sites from a single location. The method takes two parameters: the siteurl and the number of users to get.

wp_xmlrpc_server::wp_getUsers( array $args ) #

Retrieve users.


Description

The optional $filter parameter modifies the query used to retrieve users. Accepted keys are ‘number’ (default: 50), ‘offset’ (default: 0), ‘role’, ‘who’, ‘orderby’, and ‘order’.

The optional $fields parameter specifies what fields will be included in the response array.

Top ↑

See also


Top ↑

Parameters

$args

(array)(Required)Method arguments. Note: arguments must be ordered as documented.

  • 'blog_id'
    (int) (unused)
  • 'username'
    (string)
  • 'password'
    (string)
  • 'filter'
    (array) (optional)
  • 'fields'
    (array) (optional)


Top ↑

Return

(array|IXR_Error) users data


Top ↑

Source

File: wp-includes/class-wp-xmlrpc-server.php

	public function wp_getUsers( $args ) {
		if ( ! $this->minimum_args( $args, 3 ) ) {
			return $this->error;
		}

		$this->escape( $args );

		$username = $args[1];
		$password = $args[2];
		$filter   = isset( $args[3] ) ? $args[3] : array();

		if ( isset( $args[4] ) ) {
			$fields = $args[4];
		} else {
			/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
			$fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
		}

		$user = $this->login( $username, $password );
		if ( ! $user ) {
			return $this->error;
		}

		/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
		do_action( 'xmlrpc_call', 'wp.getUsers', $args, $this );

		if ( ! current_user_can( 'list_users' ) ) {
			return new IXR_Error( 401, __( 'Sorry, you are not allowed to list users.' ) );
		}

		$query = array( 'fields' => 'all_with_meta' );

		$query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
		$query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;

		if ( isset( $filter['orderby'] ) ) {
			$query['orderby'] = $filter['orderby'];

			if ( isset( $filter['order'] ) ) {
				$query['order'] = $filter['order'];
			}
		}

		if ( isset( $filter['role'] ) ) {
			if ( get_role( $filter['role'] ) === null ) {
				return new IXR_Error( 403, __( 'Invalid role.' ) );
			}

			$query['role'] = $filter['role'];
		}

		if ( isset( $filter['who'] ) ) {
			$query['who'] = $filter['who'];
		}

		$users = get_users( $query );

		$_users = array();
		foreach ( $users as $user_data ) {
			if ( current_user_can( 'edit_user', $user_data->ID ) ) {
				$_users[] = $this->_prepare_user( $user_data, $fields );
			}
		}
		return $_users;
	}

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