How to Create Custom User Roles in WordPress

User roles have been an important part of the WordPress experience always.

Lots of users even don’t know that they can create WordPress custom user roles or user types other than those default roles and limit access users for specific features of your WordPress site.

User roles are what allow the user to control which actions other users on your website can perform and what content or feature those users have access to. By default, WordPress one site comes with six roles but many plugins are used for additional user roles, and you’re also creating your user roles.

In this WordPress related tutorial, I am going to tell you the custom ways of creating WordPress new user roles.

In WordPress, a User Role is a combination or group of:

  • Role
  • Capabilities

Each role on your website describes the unique capabilities that an account with that particular role can perform.

WordPress already has six predefined roles that we can see after installing a fresh WordPress. These are Super Admin, Administrator, Editor, Author, Contributor, and Subscriber. Each user role has different features and privileges. For example, the Subscriber user role has only read capability, the Author role has Edit, Publish and Upload files and Contributor has different capabilities, such as read, delete and edit posts.




Now when you create custom user role you need to focus on do two things which are

  • Create the role
  • Assign capabilities to that created role

Creating Custom User Roles on Your WordPress Site

Now we used add_role() function to create new roles in your WordPress site.

There are three parameters in add_role()function.

add_role(#role,#display_name,#capabilities);

#role is the unique name, #display_name is the display name, #capabilities is the features one can access

Now include this small code at the end of your Functions.php which is in your theme folder.

add_role(
‘wp_coder',
__( ‘wp_coder' ),
array(
‘read'         => true,  // true allows this features or capability
‘edit_posts'   => true,
'create_posts' => true,
'edit_pages'   => true,
)
);

Now for restricting the new user for access some,capabilities you need to do only this

add_role(
 ‘wp_coder',
 __( ‘wp_coder' ),
 array(
 ‘read' => true, // true allows this features or capability 
‘edit_posts' => true, 
'create_posts' => true, 
'edit_pages' => true,
'edit_themes' => false, // false denies this features or capability.
'install_plugins' => false,
 )
 );

Now save the file, login to your site, then go to Settings then click on General. You can see the newly created user role in the list of the user.

Also, read:

I hope this tutorial will be very helpful for a beginner.


Leave a Reply

Your email address will not be published. Required fields are marked *