WordPress at Your Fingertips

get_userdata() WP 0.71

Retrieve user data by user ID as WP_User object.

The returned data fully corresponds to the fields of the database tables 'wp_users' and 'wp_usermeta'. The description of the tables see here.

Some useful values of the wp_users and `wp_usermeta ' tables fields:

users
	ID
	user_login
	user_pass
	user_nicename
	user_email
	user_url
	user_registered
	display_name

user_meta
	user_firstname
	user_lastname
	nickname
	user_description
	wp_capabilities       array
	admin_color           The theme of the admin panel. By default: fresh
	closedpostboxes_page
	primary_blog
	rich_editing
	source_domain

This is a pluggable function, and it can be replaced by a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.

Function replacement (override) — in a plugin you can create a function with the same name, then it replace this function.

1 time — 0.000296 sec (fast) | 50000 times — 0.78 sec (very fast) | PHP 7.1.2RC1, WP 4.7.2

No Hooks.

Return

WP_User|false. WP_User object. false — if the specified user could not be found.

Since version 3.2, the returned data has slightly changed: now the function returns WP_User object. The data in the object is separated into groups: data, caps, roles (previously, the data was returned in a general list). However, thanks to the "PHP magic methods", the data can be obtained as before. For example, now the data is stored like this: get_userdata()->data->rich_editing, but you can get it like this: get_userdata()->rich_editing, despite the fact that var_dump() will not show this relationship.

Usage

get_userdata( $user_id );
$user_id(int) (required)
User ID

Examples

#1 How to output data from the received data object

$user_info = get_userdata(1);

echo 'User Login: ' . $user_info->user_login . "\n";
echo 'Access level: ' . $user_info->user_level . "\n";
echo 'ID: ' . $user_info->ID . "\n";

/* Displays:
User Login: admin
Access level: 10
ID: 1
*/

#2 The data to the variable

In this example we will write the data to variables first, and then use this variable to get needed user info and display it on the screen:

$user       = get_userdata( 1 );
$username   = $user->user_login;
$first_name = $user->first_name; // Accessing Usermeta Data
$last_name  = $user->last_name;  // Accessing Usermeta Data

echo "$first_name $last_name logged in to the site with $username login.";

/*
$user object:

WP_User Object(
	[data] => stdClass Object(
			[ID]                  => 80
			[user_login]          => kogian
			[user_pass]           => $P$BJFHKJfUKyWv1TwЛОВАЕnYU0JGNsq.
			[user_nicename]       => kogian
			[user_email]          => kogian@yandex.ru
			[user_url]            => http://example.com/
			[user_registered]     => 2016-09-01 00:34:42
			[user_activation_key] =>
			[user_status]         =>
			[display_name]        => kogian
		)

	[ID] => 80
	[caps] => Array(
			[subscriber] => 1
		)

	[cap_key] => wp_capabilities
	[roles] => Array(
			[0] => subscriber
		)

	[allcaps] => Array(
			[read] => 1
			[level_0] => 1
			[subscriber] => 1
		)

	[filter] =>
	[site_id:WP_User:private] => 1
)
*/

#3 WP_User class methods

The received by get_userdata() object is an instance of the WP_User class and we can use some of useful methods. Here is a simple example of getting a user option, using the $user->get() method:

$user = get_userdata(1);
echo $username = $user->get('user_login');

List of some useful methods:

Function Description
WP_User::get( $key ) Retrieve the value of a property or meta key.
WP_User::has_prop( $key ) Determine whether a property or meta key is set
WP_User::has_cap( $cap, ...$args ) Returns whether the user has the specified capability.
WP_User::get_role_caps( ) Retrieves all of the capabilities of the user's roles, and merges them with individual user capabilities.
WP_User::add_role( $role ) Add role to user.
WP_User::remove_role( $role ) Remove role from user.
WP_User::set_role( $role ) Set the role of the user.

Changelog

Since 0.71 Introduced.

Code of get_userdata() WP 5.8.1

function get_userdata( $user_id ) {
	return get_user_by( 'id', $user_id );
}

Users (_user)

Other

No comments
    Log In