Title: Programatically changing user roles
Last modified: August 20, 2016

---

# Programatically changing user roles

 *  Resolved [danhgilmore](https://wordpress.org/support/users/danhgilmore/)
 * (@danhgilmore)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programatically-changing-user-roles/)
 * My predecessor had new users created and given a custom role, called “Blog Owner”.
   In wp_usermeta, for the record ‘wp_blogid_capabilities’, the value is ‘a:1:{s:
   5:”owner”;b:1;}’. I want to change it to ‘a:1:{s:13:”administrator”;b:1;}’. I
   know I could do a MySQL UPDATE query, but I’d rather do it programatically within
   WordPress using their functions.
 * I first query wp_usermeta for every record with the ‘owner’ value (9305 results
   returned), and I then pull the blog_id out of the meta_key (i.e., wp_23_capabilities).
   What would be the best way to update the users’ role in the associated blog_id?
 * Sorry if this doesn’t explain it well. Let me know if you have questions.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [Helen Hou-Sandi](https://wordpress.org/support/users/helen/)
 * (@helen)
 * Core Lead Developer and 4.0, 4.7, and 5.6 Release Lead
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programatically-changing-user-roles/#post-2344643)
 * Does [http://wordpress.stackexchange.com/questions/4725/how-to-change-a-users-role](http://wordpress.stackexchange.com/questions/4725/how-to-change-a-users-role)
   provide any new leads? Don’t know how that would go over in multisite.
 *  Thread Starter [danhgilmore](https://wordpress.org/support/users/danhgilmore/)
 * (@danhgilmore)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programatically-changing-user-roles/#post-2344885)
 * Revisiting this again today.
 * I have the following code:
 *     ```
       $results = $wpdb->get_results("select * from wp_usermeta where meta_value= 'a:1:{s:5:\"owner\";b:1;}';", ARRAY_A);
       if (is_array($results))
       {
       	foreach ($results as $result)
       	{
           $blogid = mb_substr($result['meta_key'], strpos($result['meta_key'], '_') +1, strrpos($result['meta_key'], '_') -3 );
           $user_id = $result['user_id'];
           echo 'Need to promote userid '. $userid . ' to Administrator on blogid  ' . $blogid .'<br>';
         }
       }
       ```
   
 * So you see I have the userID and blogID. Is there an easy function like promote(
   userID, blogID)?
 *  Thread Starter [danhgilmore](https://wordpress.org/support/users/danhgilmore/)
 * (@danhgilmore)
 * [14 years, 10 months ago](https://wordpress.org/support/topic/programatically-changing-user-roles/#post-2344892)
 * Wow, I went and over-thought the problem. Here’s the solution I came up with:
 *     ```
       switch_to_blog($blogid);
        $user = new WP_User($user_id);
        $user->remove_role('owner');
        $user->add_role('administrator');
        restore_current_blog();
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Programatically changing user roles’ is closed to new replies.

 * In: [Networking WordPress](https://wordpress.org/support/forum/multisite/)
 * 3 replies
 * 2 participants
 * Last reply from: [danhgilmore](https://wordpress.org/support/users/danhgilmore/)
 * Last activity: [14 years, 10 months ago](https://wordpress.org/support/topic/programatically-changing-user-roles/#post-2344892)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
