dots Created with Sketch.
Yogesh Chauhan's Blog

How to add a new role in WordPress?

in WordPress on July 18, 2021

User roles and user capabilities are very important aspects when you’re developing a theme or a plugin.

The user role and privileges allow users to perform some action while restricts them to perform certain actions as well.

By default, WordPress stores the user roles and user capabilities in the options table (with user_roles key).

Default WordPress Roles

There are 6 roles by default in WordPress.

  1. Super Admin
  2. Administrator
  3. Editor
  4. Author
  5. Contributor
  6. Subscriber

Since WordPress is flexible, you can remove the default roles or add additional roles as well.

add_role()

To add a new role you need to use add_role() function.

Here’s the syntax:


add_role( 
  $user_role, 
  $display_name, 
  $capabilities 
)


Where $user_role and $display_name are required parameters as strings.

In $capabilities parameters, we can pass an array with boolean values and it’s an optional parameter. You can pass a parameter like this:


array(
  'edit_posts' => true, 
  'delete_posts' => false 
)


If the roles already exists then the function will simply return null but if it doesn’t exist then the function will return the role object you added.

Example

This example function will add a new user role “Blog Admin Temp” with editor capabilities.


function yc_role() {
    add_role(
        'guest_editor',
        'Guest Editor',
        array(
            'read'         => true,
            'edit_posts'   => true,
            'upload_files' => true,
        ),
    );
}
// Add the yc_role.
add_action( 'init', 'yc_role' );


This is a simple example. You can add more capabilities and make the role a bit more complex.

Here’s the complete list of Roles and Capabilities.

Create a new user role on action

This function will create a new role when a plugin is activated. We can use register_activation_hook to do so.


function add_roles_on_plugin_activation() {
       add_role(
        'guest_editor',
        'Guest Editor',
        array(
            'read'         => true,
            'edit_posts'   => true,
            'upload_files' => true,
        ),
    );
}
register_activation_hook( 
  __FILE__, 
  'add_roles_on_plugin_activation' 
);


Remove a user role


function user_role_remove() {
    remove_role( 'guest_editor' );
}
 
// Remove the simple_role.
add_action( 'init', 'user_role_remove' );


Once you remove the role, there is no need to call the function again. It won’t do anything anyway.

amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jul 22 Is there a CSS parent selector? #Jul 22 Difference between :where and :is in CSS #Jul 22 Does :is() pseudo selector hint at CSS preprocessing in the future? #Jul 22 Control Scrolling with CSS Scroll Snap #Jul 21 Control rendering using CSS content-visibility property #Jul 21 How to use @supports rule in CSS?
You might also like these
Some SQL LIKE Operators We Need to Keep in MindSQL/MySQLHow to concatenate variable with string in Swift?SwiftFew more :nth-child examples in CSSCSSHow to define constants in PHP?PHPHow to verify your domain in Google Console with a TXT record?MiscellaneousHow to vertically and horizontally align text and image block (without flex or grid) in CSS?CSS