Adding User Role Label Next to Comments in WordPress

Wondering like is it possible to user role label next to comment?. Yes, obviously, you can add user role label next to comments in WordPress. Read the entire article to know how to add it.

The user role actually identifies the user exactly by their role. In simple, if an author replies to the comments, it would mention as “Author” immediately next to his name. The same concept applies to the rest of the roles that is been assigned. In a blog post, comments play an important factor to interact with the readers.

Comments can be given by anyone. But you can add additional weight to your comments and the reply given by your team by making it “official” responses from your website. One possible way to do is to label those comments with the WordPress user role.  

By default, WordPress has six user roles such as Super Admin, Administrator, Editor, Author, Contributor, and Subscriber.

Super Admin→ This role is only for the multi-site network. The person with this role is responsible for all the websites within a network and can access all the website features. They have the authority to edit or delete the other users and even the administrator.

Admininstrator→ This role is the most crucial role in WordPress. This role is like the owner of the website. The person with this role has all the accessibility to editing the WordPress settings, features, roles etc. It is like the person is responsible for the entire changes.

Editor→ This role is responsible for managing and creating the website content. Editors have the authority to create, delete, and edit the content with their own content as well as others content too. Their like head for the Contributor, Author, and Subscriber. The editors don’t have the authority to add, edit or delete the general WordPress settings, plugins, and widgets and they don’t have to capability to add or delete any users.

Author → This user role has the ability to write, draft, and publish the content, as well as can access the media library. They even have the authority to edit the comments, but only in their own posts. Authors don’t have to ability to access the pages or posts of other user roles and cannot create new categories, change the theme, or add new plugins on the blog or website.  

Contributor→ A person assigned to this role can just write an article in the blog post but cannot publish it. They have to drop the written article in the draft for administrator or editor to review it.  This user role doesn’t have the ability to alter, approve or delete any comments.

Subscriber→ This is the basic user role and you can assign it to anyone. It is actually a default user role, WordPress will automatically assign this role to anyone who is new to your website. It’s just like a user, who can log in, access your website or blog, leave a comment, change a password, and create or update their profile.

Now let’s see how to add user role label next to comments in WordPress

Adding user role label next to comments in WordPress

Adding the user role label next to comments builds up the trust and increases the user engagement in comments on your website.

Step 1: Login to your cPanel and click on the file manager option to view the files and folders stored on the server

file manager

Step 2: Navigate to the WordPress folder and then to wp-content > themes folder. Then open your current working theme and then select the functions.php file and then click the Edit button.

functions file

Step 3: Open the file, scroll down till the last, and enter the following lines of code:

/**
* Add User Role to Comments
*/
if ( ! class_exists(‘Comment_Author_Role_Label’) ) :
class Comment_Author_Role_Label {
public function __construct() {
add_filter( ‘get_comment_author’, array( $this, ‘get_comment_author_role’ ), 10, 3 );
add_filter( ‘get_comment_author_link’, array( $this, ‘comment_author_role’ ) );
}
function get_comment_author_role($author, $comment_id, $comment) {
$authoremail = get_comment_author_email( $comment);
if (email_exists($authoremail)) {
$commet_user_role = get_user_by( ‘email’, $authoremail );
$comment_user_role = $commet_user_role->roles[0];
$this->comment_user_role = ‘ <span class=

“comment-author-label comment-author-label- ‘.$comment_user_role.’”>’ . ucfirst($comment_user_role) . ‘</span>’;
} else {
$this->comment_user_role =”;
}
return $author;
}            
function comment_author_role($author) {
return $author .= $this->comment_user_role;
}
}
new Comment_Author_Role_Label;
endif;

 

That’s it. Save the file and run your website. You will be able to see the changes done. The registered users will display the user role next to the comments, whereas the for the non-registered users will display only the comment.

comment label

You can even add styles to have a clean look with any clumsy. You can add the following CSS to your file:

{
padding: 5px;
font-size: 14px;
border-radius: 3px;
}
.comment-author-label-editor

{
background-color:#efefef;
}
.comment-author-label-author {
background-color:#faeeee;
}
.comment-author-label-contributor

{
background-color:#f0faee;
}
.comment-author-label-subscriber

{
background-color:#eef5fa;
}
.comment-author-label-administrator

{
background-color:#fde9ff;
}

 

final user comment

This is how you can add the user role label next to comments in WordPress and style it. It is good to add user roles in comments to increase the trust. Hope you understood how to User Role Label Next to Comment in WordPress. If you have any queries or suggestions, please feel free to comment to us. You can subscribe to us on Facebook and Twitter.

 

1 thought on “Adding User Role Label Next to Comments in WordPress”

Leave a Comment