Removing unnecessary Wordpress Roles

published: November 21, 2020

A recent wordpress job I have worked on involved a significant amount of security testing for a Wordpress install using Woocommerce, YAOST and a few other smaller plugins. Entire reports and testing environemtns had to be setup for each user role possible in the database. Both Yoast and Woocommerce setup custom roles by default, and rather than do any paper work on redundant roles, I decided to just remove them. As we were using only the Customer and Adminstrator, I needed to find a way to remove the following roles.

  • Subscriber
  • Contributor
  • Author
  • Editor
  • Shop Manager
  • WPSEO Manager
  • WPSEO Editor

The following code did the trick. Add this to any custom plugin file or the funcitons.php file within your theme.

add_action('admin_menu', 'remove_unnecessary_roles');

function remove_unnecessary_roles()
{
	global $wp_roles;

	$roles_to_remove = array('subscriber', 'contributor', 'author', 'editor', 'shop_manager', 'wpseo_manager', 'wpseo_editor');

	foreach ($roles_to_remove as $role) {
		if (isset($wp_roles->roles[$role])) {
			$wp_roles->remove_role($role);
		}
	}
}
SHARE THIS ARTICLE:
Profile picture

Written by Nick La Rosa, a full stack web developer from Sydney.
Follow Nick La Rosa on Twitter