* User API: WP_User class
* Core class used to implement the WP_User object.
* @property string $nickname
* @property string $description
* @property string $user_description
* @property string $first_name
* @property string $user_firstname
* @property string $last_name
* @property string $user_lastname
* @property string $user_login
* @property string $user_pass
* @property string $user_nicename
* @property string $user_email
* @property string $user_url
* @property string $user_registered
* @property string $user_activation_key
* @property string $user_status
* @property int $user_level
* @property string $display_name
* @property string $deleted
* @property string $locale
* @property string $rich_editing
* @property string $syntax_highlighting
* Capabilities that the individual user has been granted outside of those inherited from their role.
* @var bool[] Array of key/value pairs where keys represent a capability name
* and boolean values represent whether the user has that capability.
* User metadata option name.
* The roles the user is part of.
* All capabilities the user has, including individual and role based.
* @var bool[] Array of key/value pairs where keys represent a capability name
* and boolean values represent whether the user has that capability.
public $allcaps = array();
* The filter context applied to user data fields.
* The site ID the capabilities of this user are initialized for.
private static $back_compat_keys;
* Retrieves the userdata and passes it to WP_User::init().
* @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
* @param string $name Optional. User's username
* @param int $site_id Optional Site ID, defaults to current site.
public function __construct( $id = 0, $name = '', $site_id = '' ) {
if ( ! isset( self::$back_compat_keys ) ) {
$prefix = $GLOBALS['wpdb']->prefix;
self::$back_compat_keys = array(
'user_firstname' => 'first_name',
'user_lastname' => 'last_name',
'user_description' => 'description',
'user_level' => $prefix . 'user_level',
$prefix . 'usersettings' => $prefix . 'user-settings',
$prefix . 'usersettingstime' => $prefix . 'user-settings-time',
if ( $id instanceof WP_User ) {
$this->init( $id->data, $site_id );
} elseif ( is_object( $id ) ) {
$this->init( $id, $site_id );
if ( ! empty( $id ) && ! is_numeric( $id ) ) {
$data = self::get_data_by( 'id', $id );
$data = self::get_data_by( 'login', $name );
$this->init( $data, $site_id );
$this->data = new stdClass;
* Sets up object properties, including capabilities.
* @param object $data User DB row object.
* @param int $site_id Optional. The site ID to initialize for.
public function init( $data, $site_id = '' ) {
$this->ID = (int) $data->ID;
$this->for_site( $site_id );
* Return only the main user fields
* @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
* @global wpdb $wpdb WordPress database abstraction object.
* @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'.
* @param string|int $value The field value
* @return object|false Raw user object
public static function get_data_by( $field, $value ) {
// 'ID' is an alias of 'id'.
// Make sure the value is numeric to avoid casting objects, for example,
if ( ! is_numeric( $value ) ) {
$user_id = wp_cache_get( $value, 'userslugs' );
$db_field = 'user_nicename';
$user_id = wp_cache_get( $value, 'useremail' );
$db_field = 'user_email';
$value = sanitize_user( $value );
$user_id = wp_cache_get( $value, 'userlogins' );
$db_field = 'user_login';
if ( false !== $user_id ) {
$user = wp_cache_get( $user_id, 'users' );
"SELECT * FROM $wpdb->users WHERE $db_field = %s LIMIT 1",
update_user_caches( $user );
* Magic method for checking the existence of a certain custom field.
* @param string $key User meta key to check if set.
* @return bool Whether the given user meta key is set.
public function __isset( $key ) {
/* translators: %s: WP_User->ID */
'<code>WP_User->ID</code>'
if ( isset( $this->data->$key ) ) {
if ( isset( self::$back_compat_keys[ $key ] ) ) {
$key = self::$back_compat_keys[ $key ];
return metadata_exists( 'user', $this->ID, $key );
* Magic method for accessing custom fields.
* @param string $key User meta key to retrieve.
* @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID.
public function __get( $key ) {
/* translators: %s: WP_User->ID */
'<code>WP_User->ID</code>'
if ( isset( $this->data->$key ) ) {
$value = $this->data->$key;
if ( isset( self::$back_compat_keys[ $key ] ) ) {
$key = self::$back_compat_keys[ $key ];
$value = get_user_meta( $this->ID, $key, true );
$value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
* Magic method for setting custom user fields.
* This method does not update custom fields in the database. It only stores
* the value on the WP_User instance.
* @param string $key User meta key.
* @param mixed $value User meta value.
public function __set( $key, $value ) {
/* translators: %s: WP_User->ID */
'<code>WP_User->ID</code>'
$this->data->$key = $value;
* Magic method for unsetting a certain custom field.
* @param string $key User meta key to unset.
public function __unset( $key ) {
/* translators: %s: WP_User->ID */
'<code>WP_User->ID</code>'
if ( isset( $this->data->$key ) ) {
unset( $this->data->$key );
if ( isset( self::$back_compat_keys[ $key ] ) ) {
unset( self::$back_compat_keys[ $key ] );
* Determine whether the user exists in the database.
* @return bool True if user exists in the database, false if not.
public function exists() {
return ! empty( $this->ID );
* Retrieve the value of a property or meta key.
* Retrieves from the users and usermeta table.
* @param string $key Property
public function get( $key ) {
return $this->__get( $key );
* Determine whether a property or meta key is set
* Consults the users and usermeta tables.
* @param string $key Property
public function has_prop( $key ) {
return $this->__isset( $key );
* Return an array representation.
* @return array Array representation.
public function to_array() {
return get_object_vars( $this->data );
* Makes private/protected methods readable for backward compatibility.
* @param string $name Method to call.
* @param array $arguments Arguments to pass when calling.
* @return mixed|false Return value of the callback, false otherwise.
public function __call( $name, $arguments ) {
if ( '_init_caps' === $name ) {
return $this->_init_caps( ...$arguments );
* Set up capability object properties.
* Will set the value for the 'cap_key' property to current database table
* prefix, followed by 'capabilities'. Will then check to see if the
* property matching the 'cap_key' exists and is an array. If so, it will be
* @deprecated 4.9.0 Use WP_User::for_site()
* @global wpdb $wpdb WordPress database abstraction object.
* @param string $cap_key Optional capability key
protected function _init_caps( $cap_key = '' ) {
_deprecated_function( __METHOD__, '4.9.0', 'WP_User::for_site()' );
if ( empty( $cap_key ) ) {
$this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities';
$this->cap_key = $cap_key;
$this->caps = $this->get_caps_data();
* Retrieves all of the capabilities of the user's roles, and merges them with
* individual user capabilities.
* All of the capabilities of the user's roles are merged with the user's individual
* capabilities. This means that the user can be denied specific capabilities that
* their role might have, but the user is specifically denied.
* @return bool[] Array of key/value pairs where keys represent a capability name
* and boolean values represent whether the user has that capability.
public function get_role_caps() {
if ( is_multisite() && get_current_blog_id() != $this->site_id ) {