How to Delete User Roles from a WordPress Site
There are a number of reasons why you may have user roles on your site that you don’t need or don’t want. Perhaps you decided to try a new user system that you’ve now abandoned. Or maybe you’ve installed a plugin or a specialized theme that automatically created new roles.
While plugins or themes that create roles often let you delete those roles, maybe you uninstalled them so long ago you can’t even remember what created them in the first place.
Below we’ll go over an easy way to delete those roles.
Deleting Roles… Plugin or Code
In order to delete a role, you can go for either the code solution or use a plugin.
If you’re after using a plugin, the User Role Editor plugin is a well-loved solution that should make your life easier.
Alternatively (and if you’d prefer not to use yet another plugin) just put the following into your functions file. (Appearance > Theme Functions – functions.php)
Of course, you’ll want to substitute “your_role” with the name of the role you want to delete.
$wp_roles = new WP_Roles(); $wp_roles->remove_role("your_role")
Update your functions file, and the role will be deleted. Afterward, you can take that code out of your functions file and save it again.
Once the role has been deleted, it won’t come back simply because you removed the code, so be sure that you really want to delete that role.
If you want to delete more than one role, then just repeat the second line however many times you need to with the name of the other roles.
$wp_roles = new WP_Roles(); $wp_roles->remove_role("your_role") $wp_roles->remove_role("second_role_to_delete")
Displaying User Roles
In most cases, you will know what name to put in the code above because it’s probably the same name as you see in the roles dropdown box in your users area.
For example, if you have a role called “Job Lister,” then the slug you will need to put in the code above is probably “job_lister” with an underscore in between the two words.
Sometimes, however, you may have a situation where the name and the slug are not the same.
Here’s a quick trick to display all the roles you have on your site as well as their slugs. You can put the following code into any template file, and it will display where you put it.
<?php $wp_roles = new WP_Roles(); $names = $wp_roles->get_names(); print_r($names); ?>
For example, you could put this code into your footer.php file (Appearance > Footer – footer.php), update the file, quickly copy it, then remove the code from the footer file and update it again.
Is it dirty? Yes. Is it quick? Most definitely.
Here’s a look an example of a footer file I did this to.
You can see the slugs for each role inside the brackets […]. For example, the “Job Lister” role looks like this:
[job_lister] => Job Lister
This tells me that I need to put “job_lister” into my code above to get rid of the Job Lister role.
And that’s it. Quick and effective.Tags: