Are you looking for a way to add and remove user roles in WordPress? While there’s probably a plugin for this, we have created a quick code snippet that you can use to add or remove WordPress user roles.
You can view the WordPress roles and capabilities page for more extensive details on the capabilities available that you can assign to a custom role.
Note: If you remove an existing role, then it will be gone!
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
function wps_add_role() { add_role( 'manager', 'Manager', array( 'read', 'edit_posts', 'delete_posts', ) ); } add_action( 'init', 'wps_add_role' );
function wps_remove_role() { remove_role( 'editor' ); remove_role( 'author' ); remove_role( 'contributor' ); remove_role( 'subscriber' ); } add_action( 'init', 'wps_remove_role' );
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 25 best WooCommerce plugins to grow your sales and how to increase maximum file upload size in WordPress.
Very helpful… But, can we restrict roles attribution based on a custom role? For example, can we make the manager able to add only subscribers and editor, but not authors, seo_manager or whatever?
Very interesting. Can we add a Manager role that will be able to see only the appearance section, not the plugin section?
Take a look at edit_themes and some of the options to see what you can do.