WP_REST_Users_Controller::get_user() WordPress Method

The WP_REST_Users_Controller::get_user() method is used to retrieve a single user from WordPress. This is done by specifying the user's ID in the request. The user's data is then returned as a JSON object.

WP_REST_Users_Controller::get_user( int $id ) #

Get the user, if the ID is valid.


Parameters

$id

(int)(Required)Supplied ID.


Top ↑

Return

(WP_User|WP_Error) True if ID is valid, WP_Error otherwise.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

	protected function get_user( $id ) {
		$error = new WP_Error(
			'rest_user_invalid_id',
			__( 'Invalid user ID.' ),
			array( 'status' => 404 )
		);

		if ( (int) $id <= 0 ) {
			return $error;
		}

		$user = get_userdata( (int) $id );
		if ( empty( $user ) || ! $user->exists() ) {
			return $error;
		}

		if ( is_multisite() && ! is_user_member_of_blog( $user->ID ) ) {
			return $error;
		}

		return $user;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.2Introduced.

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.