Creating a WordPress custom user role without a Plugin

this guide is for the folks who wish to create a custom user role without using any plugin

2 min read
Creating a WordPress custom user role without a Plugin

There are many plugins in WordPress repository to create a Custom user role , most popular of them like Members by Justin Tadlock and User Role Editor.

That said , this guide is for the folks who wish to create a custom user role without using any plugin . I am going to cover

  1. Creating a user role
  2. Setting Capabilities
  3. Removing User role

Generally, WordPress has 6 user roles by default:

  1. Administrator: The Supreme profile that has access to all kind of administrative activities in your WordPress site. When you create a website, WordPress sets your profile as Administrator by default.
  2. Editor: This user profile can create/edit/publish their own posts as well as others’ posts. They are responsible for managing the overall content of the WordPress site.
  3. Author: User assigned with this role can create posts, edit those posts, and manage comments in their own posts only.
  4. Contributor: This role allows the user to create/edit their own post, make a draft of it but can not publish them. Usually, this role indicates that the user is probably an external writer for your blog.
  5. Subscriber: A subscriber role allows to manage a user’s own profile only. This role allows users to only read or comment on posts.
  6. Super Admin: A role that is applicable only when you use the WordPress multisite feature. This user role is assigned to manage the network of all sites and it’s users in a multisite network. They can also create/delete or upgrade network and more.

Creating a User role :

We need to add code in similar format to create a new user role

I am creating a simple user role for a membership site , Let's assume it as gold member.

We have successfully created the user role Gold member , you can have a look here :

Created New user role Gold Member

Setting Capabilities :

You can check the full list of full capabilities a user can have in WordPress have here

For an existing User Role, if you wish to add or remove capabilities, you need to follow the previous codes but instead of assigning capabilities in the array, you need to include the following code format:

Add a Capability:

$role = get_role( 'gold_member' ),

$role->add_cap( 'install_plugins' ),

Remove a Capability:

$role = get_role( 'gold_member' ),

$role->remove_cap( 'read' ),

**The ‘get_role’ function fetches the list of capabilities of a user role. In this case, it will fetch the capabilities of the role ‘gold_member’.

Remove a User role :

Let’s say we need to remove  ‘Subscriber’ and ‘Author’ role from the user panel.

To do so, you simply need to add the following codes at the end of the functions.php file (same as you added the codes to add a user role):

remove_role( 'subscriber' );

remove_role( 'author' );

Then update and the roles will be removed. You can go to Users > All Users and check to confirm that your chosen user role(s) have been deleted.

How to add this snippet?

You can add this snippet to your child themes functions.php , We don't recommend adding code to your main theme if its not developed by you. Otherwise you can use a simple plugin

Express your views in the comment section below.


🎉 You've successfully subscribed to NCY Design !