Complete Guide to Adding User Role Label in Comment Section in WordPress

Complete Guide to Adding User Role Label in Comment Section in WordPress

WordPress comes with pre-configured User Roles. So the users are automatically allotted their roles as per the preset rules. But you can change this and customize the user roles.

The changes will reflect in the comments section. This makes it easier for administrators and users to communicate with each other. Users find it easier to identify authors and moderators. And mods and authors enjoy similar benefits.

In this article, learn how to attach User role label to the comments made in the comment section.

Benefits of Adding User Role Label

There are several benefits to adding user role label next to comment author name. Particularly, when you have multiple authors on your WordPress website, it’s helpful and necessary to affix user roles.

First, the user label works like a brief introduction for the users and correspond the roles they’re assigned.

For example, if someone is a website editor and they’ve been labeled as an “Editor,” the other users on the website know that a comment or action is coming from an editor of the website.

Such labels also make the comments or actions more trustworthy. So they’re more likely to engage with the website.

Certain WordPress themes are designed to emphasize only those comments coming from trusted users. If you’re using of those themes, you must apply user role label.

The themes also come with preset user roles. This makes it easy to assign roles to users. The preset roles are:
  • Admin - This user role signifies that the user is the admin of the website and has access to the entire website. These users also tend to have the most control over the website.
  • Editor - Editor user role means an user is the editor of the website. They can write, edit, or delete anything on the website.
  • Author - These users are the ones who specifically write on the website. They can also edit and publish the posts.
  • Contributor - Contributors are those users that can write and edit content, but are unable to publish.
  • Subscriber - Subscriber user role means the user are subscribers of the website and engage with the website frequently.
Hooks, Actions, and Filters

Before you can implement user roles next to the comments in WordPress, you need to understand three key terms, which are Hooks, Actions, and Filters.
  • Hooks
Hooks are the functions used in WordPress. These are applied to the Actions or the Filters.
  • Actions
In WordPress, Actions and Functions are functions that a developer can modify readily and apply to the themes or plugins. Actions are those functions that are executed when a particular task is being performed in WordPress.
  • Filters
Filters are those functions that enable altering specific WordPress functions.

Here’s how Hooks Actions, and Filters look like in real world:

Code:
function wp1_custom_excerpt( $output )
{
if ( has_excerpt() && ! is_attachment() )
{
$output .= wp1_continue_reading_link();
}
return $output;
}
add_filter( 'get_the_excerpt', 'wp1_custom_excerpt' );
The code generates a function named “wp1_custom_excerpt” that is hooked with the filter ”get_the_excerpt”.

Adding User Role Label to Comments in WordPress

Here’s the step-by-step process to add the user role label in WordPress.

Edit the Functions.php File

The first thing you need to do is open the functions.php file of your theme. And then copy and paste the following code into it:

Code:
if ( ! class_exists( 'WPB_Comment_Author_Role_Label' ) ) :
class WPB_Comment_Author_Role_Label{
public function __construct() {
add_filter( 'get_comment_author', array( $this, 'wpb_get_comment_author_role' ), 10, 3 );
add_filter( 'get_comment_author_link', array( $this, 'wpb_comment_author_role' ) );
}// Get comment author role
function wpb_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];
// HTML output to add next to comment author name
$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;
}
// Display comment author
function wpb_comment_author_role($author){
return $author .= $this->comment_user_role;
}
}
new WPB_Comment_Author_Role_Label;
endif;
This code will hook into WordPress filters.

You can also paste the above code into a plugin if that’s your preference. But we recommend to stick with the functions.php file.

Check the Comment Section

Next, you need to navigate into the comment section of your WordPress website and check if certain comments display the user role next to them. Usually, that should be the case. Any comment made by an user who is registered and has been assigned a role will be highlighted.

Add Styling

Adding style is crucial to make the label stand out and look authoritative to the users. So you need to include some styling elements and CSS class for different user role.

Here are the codes you need to add:

Code:
.comment-author-label
{
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
{
Customize CSS, if Required

If you want to further customize the changes, you can do so by customizing the CSS. Take the help of a developer for this. Once done, save the changes and exit.

So that’s how you add user role labels to comments in WordPress.
Author
kumkumsharma
Views
2,205
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top