WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question
up vote 26 down vote accepted

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

EDIT: I really should have provided more information with this answer initially, so i'm adding more information below.

More specifically, a user's role can be set by creating an instance of the WP_user class, and calling the add_role() or remove_role() methods.


Change a subscribers role to editor

// NOTE: Of course change 3 to the appropriate user ID
$u = new WP_User( 3 );

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

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

Hopefully that's more helpful than my initial response, which wasn't necessarily as helpful.

share|improve this answer

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()


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

// Replace the current role with 'editor' role
$u->set_role( 'editor' );
share|improve this answer
Remember that set_role will remove the previous roles of the user and assign the new role. – shasi kanth May 3 at 12:52

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' );
share|improve this answer
I think your usage of $blogID is wrong. get_users() will use the current blog ID per default anyway. – toscho Oct 29 '12 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 Nov 26 '12 at 17:46

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


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' );
share|improve this answer

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.

share|improve this answer
wp doesn't recognize that function. I got an "undefined function" error. – Joann Dec 1 '10 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. – Coen Jacobs Dec 1 '10 at 11:46

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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