W3cubDocs

/WordPress

WP_REST_Users_Controller::get_user( int $id ): WP_User|WP_Error

Get the user, if the ID is valid.

Parameters

$idintrequired
Supplied ID.

Return

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

Source

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;
}

Changelog

Version Description
4.7.2 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_users_controller/get_user