1. Home
  2. WordPress Tutorials
  3. How to Use the User Role Label in the WordPress Comments Section

How to Use the User Role Label in the WordPress Comments Section

Do you want to use the user role label in your WordPress comments section? The user role label identifies each user by their role. For example, if the author replies to a comment, it would say “Author” next to his name. The same applies to the rest of the roles that are in use. Comments will have more authority if everyone is aware of the role.

The comments section is not always the nicest place on the Internet, but seeing a role can help. Seeing a higher ranked user like an author or admin will deter rude behavior in some cases, but not all. Today, I will demonstrate how to use the user role label in the WordPress comments section.

Why Show User Roles in the WordPress Comments Sections

Showing user roles in the comments section has its advantages. Anyone can make a username like “The_Editor” and pretend to be the real deal. Showing the user roles will make it clear to everyone who is talking. This will also make it clear when staff members are interacting with the community. Interaction and engagement are what keep visitors coming back for more. For this reason, many websites try to interact with visitors on social media platforms.

Building a strong relationship with your community will only help your website grow. Acknowledging your visitors’ point of view on a specific piece of content will show them that you are listening. This is especially true when that point of view is contradictory to your own. A discussion is what makes the comments section such a powerful tool to interact with your community. If your community is having a better experience on your website, they will tell their friends.

How to Use the User Role Label in the WordPress Comments Section

Today, I will demonstrate how to use the user role label in the WordPress comments section. This will not require any additional plugins, but you will need access to your website’s cPanel. The login information is provided by your web host when you create an account with them. I would strongly recommend creating a backup of your website before beginning. This will ensure you can revert your website back to normal, in case you make a mistake.

Let’s start by logging into the cPanel and clicking on the File Manager option. The File Manager will allow you to access all of the files related to your website.

Click on the File Manager option.

You need to locate your theme’s functions.php file. Click on the public_html directory, then click on the wp-content folder. Inside of this folder, you will find all of the content related to your website. Click on the themes folder and enter the folder of the theme you are currently using. Finally, right-click on the functions.php file and select the Edit option.

Select the "Edit" option.

A pop-up window will show up. This box will warn you to create a backup of your files before editing anything. This will ensure that you can revert your website back to when it was working if something goes wrong. Click on the “Edit” button. A new tab will open containing all of the code from the file.

Click on the "Edit" button.

Copy and paste the following code into your theme’s functions.php file:

if ( ! class_exists( ‘User_Role_Label’ ) ) :
class User_Role_Label {
public function __construct() {
add_filter( ‘get_comment_author’, array( $this, ‘get_user_role’ ), 10, 3 );
add_filter( ‘get_comment_author_link’, array( $this, ‘User_role’ ) );
}

// Get comment author role
function get_user_role($author, $comment_id, $comment) {
$authoremail = get_comment_author_email( $comment);
// Check if user is registered
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 = ‘ ‘ . ucfirst($comment_user_role) . ‘‘;
} else {
$this->comment_user_role = ”;
}
return $author;
}

// Display comment author
function User_role($author) {
return $author .= $this->comment_user_role;
}
}
new User_Role_Label;
endif;

Once you have inserted the code into the functions.php file, click on the “Save Changes” button to finish.

Click on the "Save Changes" button.

Check your comments section and you will notice the user role next to the usernames.

As you can see, the role blends in with the username. To fix this, you can add some custom CSS code. Custom CSS code can be added to your theme from the admin area of WordPress or by using a plugin of your choice. On the left-hand admin panel, click on Appearance and select the Customize option.

Click on the Additional CSS option. You will be able to place any custom CSS for your current theme here. Copy and paste the following code into the text box. Keep in mind this code can be further modified to match your theme:

.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 {
background-color:#fde9ff;
}

Click on the “Publish” button to apply the CSS.

Click on the "Publish" button.

Now visit your comments section to see the changes. Each user role will have a different color. Feel free to modify this custom CSS to use on your website.

You can now see the difference.

Congratulations, you have successfully added user role labels in your WordPress comments section. You can customize the looks at any time and if you wish to delete it, simply delete the code you added to your functions.php file.

Let Everyone Know Who is Talking

The comments section is a great place to engage with your community, but it’s important they know who is talking. Talking directly to an admin, author, or editor is very different than talking to another user. Staff members should have a certain authority when they post and it’s important to make it clear. They must also be extremely mindful of what they say because it represents the website and brand as a whole.

Do you feel like comments are more respected if the person commenting has a higher level user role? How important is it to you that other people know your user role?

Author: Ron Helms

I currently work for GreenGeeks as a Support Technician. My primary roles are supporting our VPS and Dedicated server clients, as well as performing site migrations. With experience in the web hosting industry since 2009, there is rarely a question I can’t help answer. In my spare time, I enjoy gaming and working on cars as an automotive enthusiast.

Updated on January 12, 2018

Was this article helpful?

Related Articles

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.