Create and manage user roles in WordPress – Learn WordPress from a to z

Tutorials 0 lượt xem

WordPress has been a popular choice for websites that need a content management system (CMS). Like other CMSs, WordPress provides predefined user roles to meet the requirements of admins and site managers.

Most website owners are unaware that they can provide limited access to specific users. This action ensures that only a specific group of users has access to specific admin console areas and options. The WordPress user role also helps to minimize the risk of any accident that could bring down the entire site.

create and manage user roles in wordpress

Understanding WordPress User Roles

A role is the name of the user group that will show up in your WordPress Admin Dashboard, and each of these roles provides different capabilities. Administrators can fully enable or disable these roles.

By default, WordPress has five main user roles.

  • Administrator/Manager – Administrator : Has all administrative privileges.
  • Editor – Editor : Can create, edit, publish posts by themselves and other users.
  • Author – Author : Can only create, edit, publish their own posts.
  • Contributors – Contributors : Can create and edit their posts, but cannot publish them.
  • Subscribers – Subscribers : Can only manage their profiles.

You can find your WordPress permissions right in your Dashboard.

Log in to your WordPress Admin Panel, navigate to UsersAll users .

You can see the current WordPress roles available on your site:

change user role

Create new WordPress roles

Manually create, edit, or delete WordPress user roles

WordPress allows you to get rid of default user roles and create custom roles by assigning limited privileges/ability to specific user groups (Roles).

Remove default user role

I’ll start by removing the existing roles. Remember that WordPress, by default, has five roles. And for this tutorial, I will delete all user roles except Administrator . I will use the remove_role() function to remove the role. Copy the following code and paste it at the end of the functions file in your theme or child theme.

remove_role( 'subscriber' );
remove_role( 'editor' );
remove_role( 'contributor' );
remove_role( 'author' );
<div>
<span>1</span><span>2</span><span>3</span><span>4</span>
</div>

To verify that all the WordPress user roles mentioned have been deleted, navigate to UserAll users .

You can see that except for Admin , all default WordPress roles have been removed.

delete user role

Create a new user role

For this tutorial, I will create two new user roles with the following WordPress user permissions.

Moderators – Moderators: Users can create, edit, publish their own and other WordPress users’ posts.

Newbie – Newbie: Users can only edit their profile and create new posts.

To add these custom WordPress user roles, I will use the native add_role() function with the following syntax:

  • $role : The unique name of the role.
  • $display_name : The name displayed in the WordPress Admin Panel.
  • $capabilities : Capability of the role.

Moderator

This role has permission to create, edit, and publish their own posts and those of other WordPress users. Copy the following code and paste it at the end of the functions.php file in your theme or child theme.

add_role('moderator', __(
   'Moderator'),
   array(
       'read'              =&gt; true, // Allows a user to read
       'create_posts'      =&gt; true, // Allows user to create new posts
       'edit_posts'        =&gt; true, // Allows user to edit their own posts
       'edit_others_posts' =&gt; true, // Allows user to edit others posts too
       'publish_posts'     =&gt; true, // Allows the user to publish posts
       'manage_categories' =&gt; true, // Allows user to manage post categories
       )
);
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span>
</div>

 

Newbie

This role can only edit their profile and create new posts. To add this role, copy the following code and paste it at the end of the functions.php file in your theme or child theme.

add_role('newbie', __(
   'Newbie'),
   array(
       'read'            =&gt; true, // Allows a user to read
       'create_posts'    =&gt; true, // Allows user to create new posts
       'edit_posts'      =&gt; true, // Allows user to edit their own posts
       )
);
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span>
</div>

Assign the “new member” role to the user

To assign a new role to a user, navigate to WordPress Dashboard → UsersAll users and follow the instructions below.

I have assigned a Newbie role to the user, “Dicaprio.” You can see (from the image below) that when he logs in and goes to the Dashboard, he will have restricted privileges.

add user 1

You should now be able to default to the Newbie role for every new registered visitor on your website.

general settings

Epilogue

Although it is a bit complicated, this is the fastest way I can help you. Hope you have a better understanding of how to create and manage user roles in WordPress.

If you find it interesting, you can follow the  WordPress basics section  to know more new knowledge.

Follow fanpage to receive the latest posts:  Group

Bài viết liên quan