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.


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

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
add_filter function in WordPressWordPressHow to change CSS with jQuery?CSSHow to use images instead of HTML radio buttons using CSS?CSSAngular: Templates, directives, data binding, Services and dependency injectionAngularHow to Skip or Exclude a Specific URL or File From .htaccess Rewrite Rule?PHPHow to add and remove list items using JavaScript?JavaScript