Title: remove_role not working
Last modified: August 19, 2016

---

# remove_role not working

 *  [codebanter](https://wordpress.org/support/users/codebanter/)
 * (@codebanter)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/remove_role-not-working/)
 * Not sure what is going wrong, but when I use the following code I am able to 
   add a role to a specific user, however, I am not able to remove a role using 
   the remove_role command.
 * Any ideas?
 * I can add a role using the following code:
 *     ```
       global $user_ID;
       	$user = new WP_User($user_ID);
               $user->add_role('some_role_name');
       ```
   
 * But this does not remove the role:
    `$user->remove_role('some_role_name');`
 * I’m checking what roles are associated to the user using the following code:
 *     ```
       foreach ($user->roles as $role) {
       		echo "HAS ROLE: $role<br />";
       	}
       ```
   

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

 *  Thread Starter [codebanter](https://wordpress.org/support/users/codebanter/)
 * (@codebanter)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/remove_role-not-working/#post-1358951)
 * Has anyone else had this problem?
 *  Thread Starter [codebanter](https://wordpress.org/support/users/codebanter/)
 * (@codebanter)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/remove_role-not-working/#post-1359185)
 * Solved this problem later on that same day, so decided to come back in here and
   post what I found out.
 * I’ll keep this brief since I have a page on my site that explains everything 
   in greater detail. I’ll link to the page at the end of this post.
 * Basically, inside the remove_user method contained in the WP_User class, there
   is an error. The code is checking for the existence of the specified role using
 * `empty($this->roles[$role])`
 * The problem is that $this->roles returns an index array with the roles as values.
   The code above would require a multidimensional array with the role names as 
   the keys, not the values.
 * To solve this, I created a plugin which is just a custom class that extends the
   WP_User Class and overrides the remove_role method. My new method does an in_array
   check for the existence of the role. All of the existing WP_User functionality
   remains intact, therefore you can use this new class in it’s place.
 * You can see the rest of the details and download the plugin [ here at my site ](http://codebanter.com/wp_stuff/).
 * Craig

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

The topic ‘remove_role not working’ is closed to new replies.

## Tags

 * [remove_role](https://wordpress.org/support/topic-tag/remove_role/)
 * [wp_user](https://wordpress.org/support/topic-tag/wp_user/)

 * 2 replies
 * 1 participant
 * Last reply from: [codebanter](https://wordpress.org/support/users/codebanter/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/remove_role-not-working/#post-1359185)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
