• 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

    (@codebanter)

    Has anyone else had this problem?

    Thread Starter codebanter

    (@codebanter)

    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 .

    Craig

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘remove_role not working’ is closed to new replies.