• 0
Likes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/post.php

Line Number: 190

Backtrace:

File: /home2/naijacod/public_html/application/views/post.php
Line: 190
Function: _error_handler

File: /home2/naijacod/public_html/application/controllers/Posts.php
Line: 420
Function: view

File: /home2/naijacod/public_html/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to Create a Custom User Role in WordPress

 

Man looking at a computer screen with a cat.

How to Create a Custom User Role

There are two different methods for creating a custom WordPress user role. The first is by adding some code to a custom plugin or your theme’s functions.php file. The second is by installing one of the many available plugins that enable you to create and edit roles.

The method you utilize really depends on personal preference. If you want the ability to pick and choose user capabilities in a visual manner, a plugin makes the most sense. But if you’re comfortable with code and/or don’t want other administrators to play with settings, adding a snippet may be for you.

Either way, we’ll get you started with a few code examples and a selection of plugins as well.

First, it’s recommended that you study up on the various user capabilities that WordPress offers. Also note that custom post types can have their custom capabilities as well. This can come in handy in the first example scenario we mentioned above.

Method #1: Add a Code Snippet

In this example, we’re going to create a user role named “Staff Member”. This includes all of the capabilities of the Author role, but with the additional ability to edit and publish pages.

Each capability we’re assigning to the role is listed in the snippet below and is set to “true”. The exception here is that we don’t want users with this role to delete published pages, so we’re explicitly setting this to “false” – just to be safe.

Again, this code would go into your theme’s functions.php file or optionally into a custom plugin.

/* Create Staff Member User Role */
add_role(
    'staff_member', //  System name of the role.
    __( 'Staff Member'  ), // Display name of the role.
    array(
        'read'  => true,
        'delete_posts'  => true,
        'delete_published_posts' => true,
        'edit_posts'   => true,
        'publish_posts' => true,
        'upload_files'  => true,
        'edit_pages'  => true,
        'edit_published_pages'  =>  true,
        'publish_pages'  => true,
        'delete_published_pages' => false, // This user will NOT be able to  delete published pages.
    )
);

Once we’ve saved this code and refreshed our website in the browser, we can add a new user with this role.

WordPress Add New User screen.

When our user with the Staff Member role logs in, they can see both pages and posts.

The logged-in user sees Posts and Pages.

Another alternative would be to simply add a few capabilities to the existing Author role. We can do this via the add_cap() function:

/* Upgrade the Author Role */
function author_level_up() {
    // Retrieve the  Author role.
    $role = get_role(  'author' );
    
    // Let's add a set  of new capabilities we want Authors to have.
    $role->add_cap(  'edit_pages' );
    $role->add_cap(  'edit_published_pages' );
    $role->add_cap(  'publish_pages' );
}
add_action( 'admin_init', 'author_level_up');

Both of these code snippets essentially do the same thing. However, adding the new Staff Member role might be best in cases where you already have users with the Author role and don’t want them to have extra capabilities.

Method 2: Use a Plugin

The functionality above can be easily replicated through the use of a plugin. Several have been created for this type of user role and capability management. They offer an advantage in that they utilize a GUI and add a layer of convenience to the whole process.

Let’s take a look at a few of the more popular options:

User Role Editor
With User Role Editor, adding or removing capabilities for a role is as simple as checking or unchecking a box in the settings. You also have the ability to create your own custom user roles and set whatever capabilities needed. The plugin also supports multisite installs of WordPress as well.

Screen from User Role Editor

WPFront User Role Editor
WPFront User Role Editor offers similar capabilities, letting you add or edit user roles. But it also allows administrators to migrate users from one role to another and assign multiple roles to individual users.

Screen from WPFront User Role Editor.

Advanced Access Manager
If you’re looking for something with a broader scope, Advanced Access Manager may be a good choice for you. It offers role and capability management, plus the ability to control access to front end and back end features like menus and widgets.

Screen from Advanced Access Manager.

Know Your Roles

The ability to create custom user roles is just one more reason to love WordPress. It’s something very niche, yet it can be extremely helpful when you need something that goes beyond the default roles.

So, the next time you find yourself in a situation where control of user capabilities is needed, know that you have this powerful tool at your disposal.

 

Source

Drop questions if you face any difficulty, we will solve together.