xxxxxxxxxx
/**
* Manage Roles for a User
*/
$user_id = 1;
// Getting the WP_User object
$user = get_userdata( $user_id );
// The user exists
if( $user && $user->exists() ) {
// Remove all the previous roles from the user and add this one
// This will also reset all the caps and set them for the new role
$user->set_role( 'administrator' );
// Remove the Role from the user
$user->remove_role( 'administrator' );
// Add a new role to the user, while retaining the previous ones
$user->add_role( 'administrator' );
}
wordpress
xxxxxxxxxx
$user_meta = get_userdata( $user_id );
$user_roles = $user_meta->roles; // array with all the roles the user is part of.
xxxxxxxxxx
$wp_user_object = new WP_User($current_user->ID);
$wp_user_object->set_role('editor');