How to Add Custom User Roles and Capabilities in WordPress
In this tutorial i will show you how to add wordpress role capabilities. We will add new roles and permissons. We can use use Members plugin, to create new user roles and permissions. But in this tutorial i will use my own custom code.
You know that WordPress has six predefined roles like: Super Admin, Administrator, Editor, Author, Contributor, and Subscriber. Each role is allowed to perform a set of tasks called permissions.
In this article, We are going to discuss how to add custom user role with existing roles.
Creating Custom User Roles In Your WordPress Site
To create a new user role and permission just open your theme functions.php and paste this below code in it.
Read also : How to Pass PHP Data to JavaScript in WordPress
functions.php
function add_new_user_role_permissions() {
add_role(
'seller',
__( 'Seller' ),
array(
'read' => true,
'edit_posts' => false,
'delete_posts' => false,
'manage_options' => false
) );
$role = get_role( 'seller' );
$role->add_cap( 'view_dashboard' );
}
add_action( 'admin_init', 'add_new_user_role_permissions' );
Now, go to wp-admin → Users→ All Users, click on All Users then. You can see the newly created user role seller is listed along with the default user roles.
There are three parameters in add_role()
function.
add_role( $role, $display_name, $capabilities );
- $role (string) (required): Unique name of the newly created role
- $display_name (string) (required): The name to be displayed of this role
- $capabilities (array) (optional): Capabilities that one can access using this role
That’s all, you can assign a user to this role from WordPress admin panel when you will add a new user all update a users roles and permissions.