Removing Default Roles from WordPress

By default WordPress has various roles that you can assign to different registered users on your site. However, depending on your requirement you can remove some of the roles as well.

WordPress has a built-in function called remove_role, which you can easily use on your themes’s functions.php file and get rid of some of the roles that you don’t need.

Let me give you an example. Let’s say you don’t need the “Subscriber” role for your site. All you need to do is, simply copy the following code and paste it anywhere on your theme’s functions.php file. Save the page and simply refresh your site (front-end / back-end, doesn’t really matter.).

<?php
remove_role('subscriber');
?>

The “Subscriber” role from your site would be removed.

Even though this may sound very simple but in reality it’s bit complex. What you always need to remember that is you need to mention the “Role Name” within the “remove_role” function, not the display name. Let me give you another example. Let’s say, you have installed BuddyPress on your site. By default wordPress would add bunch of roles on your site. One such role is “Keymaster”. Fact of the matter is, “Keymaster” is the display name of a role. Actual role name is “bbp_keymaster”. So, to remove “Keymaster” role, you have must use “bbp_keymaster” within the function just like this:

<?php
remove_role('bbp_keymaster');
?>

We will discuss more about the “Role Name” when I would be writing on registering custom user role on my upcoming posts.

Important Note

Adding or Removing a user role always gets saved on to your database. So generally it wouldn’t make sense to run the same function over and over again. What I mean by that is, once you add the remove_role() function on your functions.php file, it would be performing the same operation every time the site loads the functions.php file. So, if you add the code and refresh any page of your site, it should remove any specific role. Next time if you visit another page, remove_role() function would be looking for the same role you just removed (even though that role doesn’t exist anymore).

Best practice would be to use this code and refresh your site. Check (Settings > General > New User Default Role drop-down list) if your desired user role has been deleted or not. As soon as you see the role has been deleted, make sure you remove / delete this function from your functions.php file.

Let’s say, while you are creating a template/theme, you added a function to create a custom role as soon as the user activates the theme. Same way, you might want to remove your custom role as soon as the user de-activates the theme. In such case, you can use the following function to remove any desired (any) role.

<?php
function ib_remove_any_role() {
   remove_role('role_name'); }
add_action('switch_theme', 'ib_remove_any_role');
?>

This is just a logical way to show how you can use remove_role() function based on your own requirements. I hope you found this post to be useful. Thanks.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.