Codex tools: Log in
Contents |
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 :
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
}
?>
WP_User is located in wp-includes/capabilities.php.