Back to Top

How to Add User Roles and Capabilities in WordPress

User Roles in Wordpress

WordPress is one of the best and flexible content management system.In WordPress, You can create new user roles, which have specific access to various parts of the admin. after that, you can create users and associate a user to the role you created.

This tutorial will walk you through setting up new user roles in WordPress and allowing them the necessary access to features within the WordPress content management system.

See about: Share user Data in multiple WordPress Installation

Ok, we’ll start by dive into the codes in which you need to define global variable $wp_roles then after adding new roles you want to define for user using inbuilt function of WordPress add_role.

so Let’s see the code

function init_add_roles() {
	global $wp_roles;
	if (class_exists('WP_Roles')) 	
		if ( ! isset( $wp_roles ) )
			$wp_roles = new WP_Roles();	
	$wp_roles->add_role( 'role_name', __('Role Name'),
					'read' => true, // True allows read capability, false to removes it.
					'edit_posts' 	=> false,
					'delete_posts' 	=> false,
					'edit_published_posts' => true,
					'publish_posts' => true,
					'edit_files' 	=> true,
					'upload_files' 	=> true

add_action('init', 'init_add_roles');

Here, I have added a new action for adding a new role for the user. same like that you can remove user role using the inbuilt function of WordPress remove_role.

Also Like: To Get number of comments of a post

global $wp_roles;	
$wp_roles->remove_role( 'salesman');

Users and user roles are necessary parts of setting up any site that is going to be managed by more than one person.It’s pretty simple and easy, so don’t bother to use it in your development.

Thank you for reading.Feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use EXISTS and NOT EXISTS in MySQL?

Posted on 4 years ago


JavaScript Ticker for Database Values

Posted on 7 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago