54

I have custom roles in my setup and I want to be able to automatically change a user's role thru a function. Say user A has a SUBSCRIBER role, how do I change it to EDITOR? When adding a role we just:

add_role( $role_name , $role_display_name , array( 'read' =>  true,
                                                   'edit_posts' => false,
                                                   'delete_posts' => false, ));

How about changing a role? Is there something like:

change_role($old_role, $new_role);

UPDATE: I think this one will do:

$wp_user_object = new WP_User($current_user->ID);
$wp_user_object->set_role('editor');

9 Answers 9

73

See the WP_User class, you can use this to add and remove roles for a user.

Specifically, a user's role can be modified by creating an instance of the WP_User class, and calling the add_role, remove_role or set_role methods depending on what your requirements are.

Example

Removing the subscriber role, then adding the editor role.

// Making sure to adjust `3` to an appropriate user ID
$u = new WP_User( 3 );

// Remove role
$u->remove_role( 'subscriber' );

// Add role
$u->add_role( 'editor' );

The add and remove methods are probably most suited to use cases where users are given multiple roles.

If your aim is to simply switch a role, this is more easily done using the set_role method, like so.

// Making sure to adjust `10` to an appropriate user ID
$u = new WP_User( 10 );

// Switch role
$u->set_role( 'author' );

Hope that helps.

4
  • remove_role() and add_rule() save data to the database?
    – b_dubb
    Commented Oct 29, 2019 at 21:42
  • 1
    Yes @b_dubb, both methods save to db through the update_user_meta() method here. See add_role() here and remove_role() here Commented Jan 7, 2020 at 12:58
  • That's pretty handy. Thanks.
    – b_dubb
    Commented Jan 7, 2020 at 21:07
  • 3
    set_role() will remove all roles and add the specified role in one command
    – G-J
    Commented Apr 18, 2020 at 22:11
33

Just note that there is a simpler way to change the user role which is especially helpful when you do not know the current role of the user: ->set_role()

Example:

// Fetch the WP_User object of our user.
$u = new WP_User( 3 );

// Replace the current role with 'editor' role
$u->set_role( 'editor' );
2
  • 1
    Remember that set_role will remove the previous roles of the user and assign the new role. Commented May 3, 2016 at 12:52
  • 1
    This is perfect for custom registration forms. First register users with no roles and after that add role when they confirm email. Commented Sep 15, 2017 at 17:34
4

To extrapolate on t31os's answer you can slap something like this in your functions file if you want to do this programmatically based on a condition

$blogusers = get_users($blogID.'&role=student');

foreach ($blogusers as $user) {

    $thisYear = date('Y-7');
    $gradYear = date(get_the_author_meta( 'graduation_year', $user->ID ).'-7');

    if($gradYear < $thisYear) {
        $u = new WP_User( $user->ID );
        // Remove role
        $u->remove_role( 'student' );

        // Add role
        $u->add_role( 'adult' );
    }
}
2
  • I think your usage of $blogID is wrong. get_users() will use the current blog ID per default anyway.
    – fuxia
    Commented Oct 29, 2012 at 19:27
  • yep, in my case the paste was just from a multisite example. I didn't define it here either so obviously it would throw an error.
    – Adam Munns
    Commented Nov 26, 2012 at 17:46
4
<?php
$wuser_ID = get_current_user_id();
if ($wuser_ID)
    {
      // NOTE: Of course change 3 to the appropriate user ID
      $u = new WP_User( $wuser_ID );

      // Remove role
      $u->remove_role( 'subscriber' );

      // Add role
      $u->add_role( 'contributor' );
    }
?>
2

You can change the role of any user by editing the users profile. No need to add any more code when this option is already built into WordPress.

enter image description here

Or

You could use code to change all current users with the subscriber role to editor:

$current_user = wp_get_current_user();

// Remove role
$current_user->remove_role( 'subscriber' );

// Add role
$current_user->add_role( 'editor' );
1

There's a WordPress function for that!

I think it is best to use WordPress functions, if and when they are available.

You can use the wp_insert_user() function, where one of the arguments that you will need to provide is the $userdata['role']. In this argument you can specify the role that you want to change the user into.

2
  • wp doesn't recognize that function. I got an "undefined function" error.
    – Joann
    Commented Dec 1, 2010 at 11:42
  • By the looks of it, wp_insert_user() seems to do the exact same. When you provide an ID, that ID gets updated. No ID is adding new user. Don't really know what the difference between wp_update_user() and wp_insert_user() is, yet. Commented Dec 1, 2010 at 11:46
1

you have to include the /wp-includes/registration.php, if you use the code as a stand alone script. Regards Uwe

1

You can use wp_update_user. Your code shoud be like this:

<?php
    $user_id = 3;
    $new_role = 'editor';

    $result = wp_update_user(array('ID'=>$user_id, 'role'=>$new_role));

    if ( is_wp_error( $result ) ) {
        // There was an error, probably that user doesn't exist.
    } else {
        // Success!
    }
?>
0

I know its a very old Post, but i have found that the roles for users are stored in wp_usermeta table with key wp_capabilities in meta_key column.

If you want to change the user role you can do it by this simple function.

function change_role($id, $new_role){
    GLOBAL $table_prefix;
    if(is_array($new_role)){
        $new_role_array = $new_role;
    }else{
        $new_role_array = array( $new_role => true );
    }
    return update_user_meta($id, $table_prefix.'capabilities', $new_role_array);
}

There is two way to use this function.

If you want to change the role for a single role.

change_role(2, 'editor'); // editor is the new role

Or if you want to add multi roles to the user, use the roles as array in the second parameter.

$roles_array = array('editor' => true, 'administrator' => true); // roles array
change_role(2, $roles_array);

Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.