Languages: English • 日本語 (Add your language)
The WordPress User Class allows accessing properties, roles and capabilities of a specific user.
To instantiate a specific user, you may use the class constructor :
<?php $users = new WP_User( $id [, $name [, $blog_id ] ] ); ?>
You can also use the wp_get_current_user global function to get a WP_User object matching the current logged user.
<?php $current_user = wp_get_current_user(); ?>
The WP_User constructor allows the following parameters :
The semantics seem rather fuzzy. If id is a WP_User or other object, or an array of properties, it seems to clone it without touching the cache. Otherwise if id is a number it seems to try to read it possibly from the cache, or from the database using ->get_data_by(), which can also update the cache. If id is empty it reads by login name.
Note: If called with the "id" or "name" parameter, the constructor queries the wp_users table. If successful, the additional row data become properties of the object: user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_key, user_status, display_name, spam (multisite only), deleted (multisite only).
These are PHP5 magic methods that facilitate property overloading. They don't need to be called directly, but are called automatically when a class property is referenced.
Magic method for accessing custom fields.
Determine whether a property or meta key is set from the users and usermeta tables.
Magic method for setting custom fields.
Please note that setting custom fields affects only the current WP_User object, and does not set the value in the database. To change the database value use wp_update_user() or update_user_meta().
Determine whether the user exists in the database.
It actually tests !empty(ID), which will normally indicate that the user record was in the database at some stage. It does not access the database.
Retrieve the value of a property or meta key from the users and usermeta tables.
Determine whether a property or meta key is set from the users and usermeta tables.
Retrieve all of the role capabilities and merge with individual capabilities.
All of the capabilities of the roles the user belongs to are merged with the users individual roles. This also means that the user can be denied specific roles that their role might have, but the specific user isn't granted permission to.
Sets the roles and allcaps properties.
Add role to user.
Updates the user's meta data option with capabilities and roles.
Remove role from user.
Set the role of the user.
This will remove the previous roles of the user and assign the user the new one. You can set the role to an empty string and it will remove all of the roles from the user.
Add capability and grant or deny access to capability.
Remove capability from user.
Remove all of the capabilities of the user.
Whether user has capability or role name.
Checking if the current user can edit user profiles :
<?php
$cu = wp_get_current_user();
if ($cu->has_cap('edit_users')) {
// do something
}
?>
Check if user exists:
<?php
$user = wp_get_current_user();
if ( $user->exists() ) {
// do something
}
?>
Check if user has a property:
<?php
$user = wp_get_current_user();
if ( $user->has_prop( 'twitter' ) ) {
// do something
}
?>
Since: 2.0.0
WP_User is located in wp-includes/class-wp-user.php
.