How to Create Multiple Custom User Roles in WordPress

WordPress comes with five default user roles.

  • Administrator: This user has access to all the administrative features and functions within a site.
  • Editor: This user can manage posts of all users and publish their own also.
  • Author: This user can only publish posts and manage posts published by them.
  • Contributor: This user can write posts but cannot post them, they can submit it for review.
  • Subscriber: This user only have access to their profile.

Except the above listed users of WordPress you can also add your own custom user name and define it’s capabilites. Below code will create a new user role “Client” with capability same as of subscribers.

<?php 
// Adds Client as new user role 
add_action( 'init', 'amn_add_role'); 

function amn_add_role(){
 add_role( 'client',
 __( 'Client' ),
 array( 'read' => true, // true allows this capability
 'edit_posts' => false,
 'edit_pages' => false,
 'manage_options' => false,
 'delete_posts' => false, // Use false to explicitly deny
 ));
}?>


How to check user role added successfully?
When you add new user you will see your role also in the dropdown list of Role.

custom user role added

 

When adding user roles using plugin you can use remove_role() with plugin deactivation hook to remove the additional role added when your plugin is deactivated.

remove_role( 'client' ); //remove user role client

Make sure no user exists with this user role before removing the role. It is not necessary to remove the role, but it’s good to remove unnecesaary things.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: