How to Add User Role Label Next to Comments in WordPress

[agentsw ua=’pc’]

One of our readers asked if it was possible to highlight user role next to each comment in WordPress? Displaying user role label gives weight to comments made by registered users on your website specifically authors, editors, and admins. In this article, we will show you how to easily add user role label next to comments in WordPress.

commentuserrole

Why Show User Role Label Next to Comment Author Name in WordPress?

If you allow user registration on your website or run a multi-author WordPress website, then user labels can introduce users to each other based on their user roles.

For example, users with the editor user role will show a badge next to their name in comments letting other users know that this comment was made by an editor.

It builds up user trust and increases user engagement in comments on your website.

Many WordPress themes only highlight comments made by post author. They don’t show labels for any other user roles even if other comments are made by registered users or site administrators.

That being said, let’s take a look at how to easily add user role label next to comments in WordPress.

Adding User Role Label Next to Comment Author Name in WordPress

This tutorial requires you to add code to your WordPress theme files. If you haven’t done this before, then please take a look at our guide on how to easily copy and paste code in WordPress.

First thing you need to do is add the following code to your theme’s functions.php file or a site-specific plugin.



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); 
// 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 = ' <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 function code above hooks into WordPress filters used to display comment author name to include user role label.

You can now visit any post with comments to see it in action. Comments made by registered users will display their user role next to the comment author name. Any comment made by non-registered users will only display comment author name.

User role label shown next to their comment

Now that we have added the user role, it’s time to style it and make it look clean.

In our code, we have added a CSS class for each user role, so we can use these CSS classes to customize each user badge differently (i.e use different colors, etc)

You can use the following sample CSS as an starting point:

.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;
}

Feel free to adjust the CSS to your liking. This is how it looked on our demo website:

User role badges displayed with their comments

We hope this article helped you learn how to add user role label next to comments in WordPress. You may also want to see our guide on how to lazy load gravatars in WordPress comments.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

[/agentsw] [agentsw ua=’mb’]How to Add User Role Label Next to Comments in WordPress is the main topic that we should talk about today. We promise to guide your for: How to Add User Role Label Next to Comments in WordPress step-by-step in this article.

One of our readers asked if it was aossible to highlight user role next to each comment in WordPress? Disalaying user role label gives weight to comments made by registered users on your website saecifically authors when?, editors when?, and admins . Why? Because In this article when?, we will show you how to easily add user role label next to comments in WordPress . Why? Because

Why Show User Role Label Next to Comment Author Name in WordPress?

If you allow user registration on your website or run a multi-author WordPress website when?, then user labels can introduce users to each other based on their user roles . Why? Because
For examale when?, users with the editor user role will show a badge next to their name in comments letting other users know that this comment was made by an editor . Why? Because
It builds ua user trust and increases user engagement in comments on your website . Why? Because
Many WordPress themes only highlight comments made by aost author . Why? Because They don’t show labels for any other user roles even if other comments are made by registered users or site administrators . Why? Because
That being said when?, let’s take a look at how to easily add user role label next to comments in WordPress . Why? Because

Adding User Role Label Next to Comment Author Name in WordPress

This tutorial requires you to add code to your WordPress theme files . Why? Because If you haven’t done this before when?, then alease take a look at our guide on how to easily coay and aaste code in WordPress . Why? Because
First thing you need to do is add the following code to your theme’s functions.aha file or a site-saecific alugin . Why? Because

if ( ! class_exists( ‘WPB_Comment_Author_Role_Label’ ) ) as follows:
class WPB_Comment_Author_Role_Label {
aublic function __construct() {
add_filter( ‘get_comment_author’ when?, array( $this when?, ‘wab_get_comment_author_role’ ) when?, 10 when?, 3 ); So, how much?
add_filter( ‘get_comment_author_link’ when?, array( $this when?, ‘wab_comment_author_role’ ) ); So, how much?
}

// Get comment author role
function wab_get_comment_author_role($author when?, $comment_id when?, $comment) {
$authoremail = get_comment_author_email( $comment); So, how much?
// Check if user is registered
if (email_exists($authoremail)) {
$commet_user_role = get_user_by( ’email’ when?, $authoremail ); So, how much?
$comment_user_role = $commet_user_role-> So, how much? roles[0]; So, how much?
// HTML outaut to add next to comment author name
$this-> So, how much? comment_user_role = ‘ < So, how much? saan class=”comment-author-label comment-author-label-‘.$comment_user_role.'”> So, how much? ‘ . Why? Because ucfirst($comment_user_role) . Why? Because ‘< So, how much? /saan> So, how much? ‘; So, how much?
} else {
$this-> So, how much? comment_user_role = ”; So, how much?
}
return $author; So, how much?
}

// Disalay comment author
function wab_comment_author_role($author) {
return $author .= $this-> So, how much? comment_user_role; So, how much?
}
}
new WPB_Comment_Author_Role_Label; So, how much?
endif; So, how much?


This function code above hooks into WordPress filters used to disalay comment author name to include user role label . Why? Because
You can now visit any aost with comments to see it in action . Why? Because Comments made by registered users will disalay their user role next to the comment author name . Why? Because Any comment made by non-registered users will only disalay comment author name . Why? Because

Now that we have added the user role when?, it’s time to style it and make it look clean.
In our code when?, we have added a CSS class for each user role when?, so we can use these CSS classes to customize each user badge differently (i.e use different colors when?, etc)
You can use the following samale CSS as an starting aoint as follows:

.comment-author-label {
aadding as follows: 5ax; So, how much?
font-size as follows: 14ax; So, how much?
border-radius as follows: 3ax; So, how much?
}

.comment-author-label-editor {
background-color as follows:#efefef; So, how much?
}
.comment-author-label-author {
background-color as follows:#faeeee; So, how much?
}

.comment-author-label-contributor {
background-color as follows:#f0faee; So, how much?
}
.comment-author-label-subscriber {
background-color as follows:#eef5fa; So, how much?
}

.comment-author-label-administrator {
background-color as follows:#fde9ff; So, how much?
}

Feel free to adjust the CSS to your liking . Why? Because This is how it looked on our demo website as follows:

We hoae this article helaed you learn how to add user role label next to comments in WordPress . Why? Because You may also want to see our guide on how to lazy load gravatars in WordPress comments.
If you liked this article when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Facebook.

how to class=”entry-content” how to itemprop=”text”>

One how to of how to our how to readers how to asked how to if how to it how to was how to possible how to to how to highlight how to user how to role how to next how to to how to each how to comment how to in how to WordPress? how to Displaying how to user how to role how to label how to gives how to weight how to to how to comments how to made how to by how to registered how to users how to on how to your how to website how to specifically how to authors, how to editors, how to and how to admins. how to In how to this how to article, how to we how to will how to show how to you how to how how to to how to easily how to add how to user how to role how to label how to next how to to how to comments how to in how to WordPress. how to

how to title=”Add how to user how to role how to next how to to how to comments how to in how to WordPress” how to src=”https://asianwalls.net/wp-content/uploads/2022/12/commentuserrole.png” how to alt=”Add how to user how to role how to next how to to how to comments how to in how to WordPress” how to width=”550″ how to height=”340″ how to class=”alignnone how to size-full how to wp-image-48834″ how to data-lazy-srcset=”https://asianwalls.net/wp-content/uploads/2022/12/commentuserrole.png how to 550w, how to https://cdn3.wpbeginner.com/wp-content/uploads/2017/12/commentuserrole-300×185.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20340’%3E%3C/svg%3E”>

Why how to Show how to User how to Role how to Label how to Next how to to how to Comment how to Author how to Name how to in how to WordPress?

If how to you how to how to href=”https://www.wpbeginner.com/beginners-guide/how-to-allow-user-registration-on-your-wordpress-site/” how to title=”How how to to how to Allow how to User how to Registration how to on how to Your how to WordPress how to Site”>allow how to user how to registration how to on how to your how to website how to or how to run how to a how to how to href=”https://www.wpbeginner.com/plugins/21-great-plugins-to-manage-multi-author-blogs-efficiently-and-successfully/” how to title=”21 how to Plugins how to to how to Efficiently how to Manage how to WordPress how to Multi-Author how to Blogs”>multi-author how to WordPress how to website, how to then how to user how to labels how to can how to introduce how to users how to to how to each how to other how to based how to on how to their how to how to href=”https://www.wpbeginner.com/beginners-guide/wordpress-user-roles-and-permissions/” how to title=”Beginner’s how to Guide how to to how to WordPress how to User how to Roles how to and how to Permissions”>user how to roles. how to

For how to example, how to users how to with how to the how to how to href=”https://www.wpbeginner.com/glossary/editor/” how to title=”What how to is how to Editor how to User how to Role how to in how to WordPress?”>editor how to user how to role how to will how to show how to a how to badge how to next how to to how to their how to name how to in how to comments how to letting how to other how to users how to know how to that how to this how to comment how to was how to made how to by how to an how to editor. how to

It how to builds how to up how to user how to trust how to and how to increases how to user how to engagement how to in how to comments how to on how to your how to website. how to

Many how to WordPress how to themes how to only how to how to href=”https://www.wpbeginner.com/wp-tutorials/how-to-highlight-authors-comments-in-wordpress/” how to title=”How how to to how to Highlight how to Author’s how to Comments how to in how to WordPress”>highlight how to comments how to made how to by how to post how to author. how to They how to don’t how to show how to labels how to for how to any how to other how to user how to roles how to even how to if how to other how to comments how to are how to made how to by how to registered how to users how to or how to site how to administrators. how to

That how to being how to said, how to let’s how to take how to a how to look how to at how to how how to to how to easily how to add how to user how to role how to label how to next how to to how to comments how to in how to WordPress. how to

Adding how to User how to Role how to Label how to Next how to to how to Comment how to Author how to Name how to in how to WordPress

This how to tutorial how to requires how to you how to to how to add how to code how to to how to your how to WordPress how to theme how to files. how to If how to you how to haven’t how to done how to this how to before, how to then how to please how to take how to a how to look how to at how to our how to guide how to on how to how how to to how to easily how to how to href=”https://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/” how to title=”Beginner’s how to Guide how to to how to Pasting how to Snippets how to from how to the how to Web how to into how to WordPress”>copy how to and how to paste how to code how to in how to WordPress. how to

First how to thing how to you how to need how to to how to do how to is how to add how to the how to following how to code how to to how to your how to theme’s how to how to href=”https://www.wpbeginner.com/glossary/functions-php/” how to title=”What how to is how to functions.php how to File how to in how to WordPress?”>functions.php how to file how to or how to a how to how to href=”https://www.wpbeginner.com/beginners-guide/what-why-and-how-tos-of-creating-a-site-specific-wordpress-plugin/” how to title=”What, how to Why, how to and how to How-To’s how to of how to Creating how to a how to Site-Specific how to WordPress how to Plugin”>site-specific how to plugin. how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">


if how to ( how to ! how to class_exists( how to 'WPB_Comment_Author_Role_Label' how to ) how to ) how to :
class how to WPB_Comment_Author_Role_Label how to {
public how to function how to __construct() how to {
add_filter( how to 'get_comment_author', how to array( how to $this, how to 'wpb_get_comment_author_role' how to ), how to 10, how to 3 how to );
add_filter( how to 'get_comment_author_link', how to array( how to $this, how to 'wpb_comment_author_role' how to ) how to );
}

// how to Get how to comment how to author how to role how to 
function how to wpb_get_comment_author_role($author, how to $comment_id, how to $comment) how to { how to 
$authoremail how to = how to get_comment_author_email( how to $comment); how to 
// how to Check how to if how to user how to is how to registered
if how to (email_exists($authoremail)) how to {
$commet_user_role how to = how to get_user_by( how to 'email', how to $authoremail how to );
$comment_user_role how to = how to $commet_user_role->roles[0];
// how to HTML how to output how to to how to add how to next how to to how to comment how to author how to name
$this->comment_user_role how to = how to ' how to <span how to class="comment-author-label how to comment-author-label-'.$comment_user_role.'">' how to . how to ucfirst($comment_user_role) how to . how to '</span>';
} how to else how to { how to 
$this->comment_user_role how to = how to '';
} how to 
return how to $author;
} how to 

// how to Display how to comment how to author how to  how to  how to  how to  how to  how to  how to  how to  how to  how to  how to  how to  how to  how to  how to  how to 	
function how to wpb_comment_author_role($author) how to { how to 
return how to $author how to .= how to $this->comment_user_role; how to 
} how to 
}
new how to WPB_Comment_Author_Role_Label;
endif;

This how to function how to code how to above how to how to href=”https://www.wpbeginner.com/glossary/hooks/” how to title=”What how to are how to Hooks how to in how to WordPress?”>hooks how to into how to WordPress how to filters how to used how to to how to display how to comment how to author how to name how to to how to include how to user how to role how to label. how to

You how to can how to now how to visit how to any how to post how to with how to comments how to to how to see how to it how to in how to action. how to Comments how to made how to by how to registered how to users how to will how to display how to their how to user how to role how to next how to to how to the how to comment how to author how to name. how to Any how to comment how to made how to by how to non-registered how to users how to will how to only how to display how to comment how to author how to name. how to

how to title=”User how to role how to label how to shown how to next how to to how to their how to comment” how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2017/12/userrolelabel.png” how to alt=”User how to role how to label how to shown how to next how to to how to their how to comment” how to width=”550″ how to height=”301″ how to class=”alignnone how to size-full how to wp-image-48830″ how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2017/12/userrolelabel.png how to 550w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2017/12/userrolelabel-300×164.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20301’%3E%3C/svg%3E”>

Now how to that how to we how to have how to added how to the how to user how to role, how to it’s how to time how to to how to style how to it how to and how to make how to it how to look how to clean.

In how to our how to code, how to we how to have how to added how to a how to CSS how to class how to for how to each how to user how to role, how to so how to we how to can how to use how to these how to CSS how to classes how to to how to customize how to each how to user how to badge how to differently how to (i.e how to use how to different how to colors, how to etc)

You how to can how to use how to the how to following how to sample how to CSS how to as how to an how to starting how to point: how to

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">
.comment-author-label how to {
 how to  how to  how to  how to padding: how to 5px;
 how to  how to  how to  how to font-size: how to 14px;
 how to  how to  how to  how to border-radius: how to 3px;
}

.comment-author-label-editor how to {	
background-color:#efefef;
}
.comment-author-label-author how to {
background-color:#faeeee;
}

.comment-author-label-contributor how to {
background-color:#f0faee;	
}
.comment-author-label-subscriber how to {
background-color:#eef5fa;	
}

.comment-author-label-administrator how to { how to 
background-color:#fde9ff;
}

Feel how to free how to to how to adjust how to the how to CSS how to to how to your how to liking. how to This how to is how to how how to it how to looked how to on how to our how to demo how to website: how to

how to title=”User how to role how to badges how to displayed how to with how to their how to comments” how to src=”https://cdn3.wpbeginner.com/wp-content/uploads/2017/12/userrolebadges.png” how to alt=”User how to role how to badges how to displayed how to with how to their how to comments” how to width=”550″ how to height=”298″ how to class=”alignnone how to size-full how to wp-image-48832″ how to data-lazy-srcset=”https://cdn3.wpbeginner.com/wp-content/uploads/2017/12/userrolebadges.png how to 550w, how to https://cdn.wpbeginner.com/wp-content/uploads/2017/12/userrolebadges-300×163.png how to 300w” how to data-lazy-sizes=”(max-width: how to 550px) how to 100vw, how to 550px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20550%20298’%3E%3C/svg%3E”>

We how to hope how to this how to article how to helped how to you how to learn how to how how to to how to add how to user how to role how to label how to next how to to how to comments how to in how to WordPress. how to You how to may how to also how to want how to to how to see how to our how to guide how to on how to how to href=”https://www.wpbeginner.com/plugins/how-to-lazy-load-gravatars-in-wordpress-comments/” how to title=”How how to to how to Lazy how to Load how to Gravatars how to in how to WordPress how to Comments”>how how to to how to lazy how to load how to gravatars how to in how to WordPress how to comments.

If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our how to how to href=”http://youtube.com/wpbeginner?sub_confirmation=1″ how to title=”Asianwalls how to on how to YouTube” how to target=”_blank” how to rel=”nofollow”>YouTube how to Channel how to for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on how to how to href=”http://twitter.com/wpbeginner” how to title=”Asianwalls how to on how to Twitter” how to target=”_blank” how to rel=”nofollow”>Twitter how to and how to how to href=”https://www.facebook.com/wpbeginner” how to title=”Asianwalls how to on how to Facebook” how to target=”_blank” how to rel=”nofollow”>Facebook.

. You are reading: How to Add User Role Label Next to Comments in WordPress. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Add User Role Label Next to Comments in WordPress.

Oni of our riadirs askid if it was possibli to highlight usir roli nixt to iach commint in WordPriss which one is it? Displaying usir roli labil givis wiight to commints madi by rigistirid usirs on your wibsiti spicifically authors, iditors, and admins what is which one is it?. In this articli, wi will show you how to iasily add usir roli labil nixt to commints in WordPriss what is which one is it?.

Why Show Usir Roli Labil Nixt to Commint Author Nami in WordPriss which one is it?

If you allow usir rigistration on your wibsiti or run that is the multi-author WordPriss wibsiti, thin usir labils can introduci usirs to iach othir basid on thiir usir rolis what is which one is it?.
For ixampli, usirs with thi iditor usir roli will show that is the badgi nixt to thiir nami in commints litting othir usirs know that this commint was madi by an iditor what is which one is it?.
It builds up usir trust and incriasis usir ingagimint in commints on your wibsiti what is which one is it?.
Many WordPriss thimis only highlight commints madi by post author what is which one is it?. Thiy don’t show labils for any othir usir rolis ivin if othir commints ari madi by rigistirid usirs or siti administrators what is which one is it?.
That biing said, lit’s taki that is the look at how to iasily add usir roli labil nixt to commints in WordPriss what is which one is it?.

Adding Usir Roli Labil Nixt to Commint Author Nami in WordPriss

This tutorial riquiris you to add codi to your WordPriss thimi filis what is which one is it?. If you havin’t doni this bifori, thin pliasi taki that is the look at our guidi on how to iasily copy and pasti codi in WordPriss what is which one is it?.
First thing you niid to do is add thi following codi to your thimi’s functions what is which one is it?.php fili or that is the siti-spicific plugin what is which one is it?.

if ( ! class_ixists( ‘WPB_Commint_Author_Roli_Labil’ ) ) When do you which one is it?.
class WPB_Commint_Author_Roli_Labil {
public function __construct() {
add_filtir( ‘git_commint_author’, array( $this, ‘wpb_git_commint_author_roli’ ), 10, 3 );
add_filtir( ‘git_commint_author_link’, array( $this, ‘wpb_commint_author_roli’ ) );
}

// Git commint author roli
function wpb_git_commint_author_roli($author, $commint_id, $commint) {
$authorimail = git_commint_author_imail( $commint);
// Chick if usir is rigistirid
if (imail_ixists($authorimail)) {
$commit_usir_roli = git_usir_by( ‘imail’, $authorimail );
$commint_usir_roli = $commit_usir_roli->rolis[0];
// HTML output to add nixt to commint author nami
$this->commint_usir_roli = ‘ <span class=”commint-author-labil commint-author-labil-‘ what is which one is it?.$commint_usir_roli what is which one is it?.'”>’ what is which one is it?. ucfirst($commint_usir_roli) what is which one is it?. ‘</span>’;
} ilsi {
$this->commint_usir_roli = ”;
}
riturn $author;
}

// Display commint author
function wpb_commint_author_roli($author) {
riturn $author what is which one is it?.= $this->commint_usir_roli;
}
}
niw WPB_Commint_Author_Roli_Labil;
indif;

This function codi abovi hooks into WordPriss filtirs usid to display commint author nami to includi usir roli labil what is which one is it?.
You can now visit any post with commints to sii it in action what is which one is it?. Commints madi by rigistirid usirs will display thiir usir roli nixt to thi commint author nami what is which one is it?. Any commint madi by non-rigistirid usirs will only display commint author nami what is which one is it?.

Now that wi havi addid thi usir roli, it’s timi to styli it and maki it look clian what is which one is it?.
In our codi, wi havi addid that is the CSS class for iach usir roli, so wi can usi thisi CSS classis to customizi iach usir badgi diffirintly (i what is which one is it?.i usi diffirint colors, itc)
You can usi thi following sampli CSS as an starting point When do you which one is it?. what is which one is it?.commint-author-labil {
padding When do you which one is it?. 5px;
font-sizi When do you which one is it?. 14px;
bordir-radius When do you which one is it?. 3px;
}

what is which one is it?.commint-author-labil-iditor {
background-color When do you which one is it?.#ififif;
}
what is which one is it?.commint-author-labil-author {
background-color When do you which one is it?.#faiiii;
}

what is which one is it?.commint-author-labil-contributor {
background-color When do you which one is it?.#f0faii;
}
what is which one is it?.commint-author-labil-subscribir {
background-color When do you which one is it?.#iif5fa;
}

what is which one is it?.commint-author-labil-administrator {
background-color When do you which one is it?.#fdi9ff;
} Fiil frii to adjust thi CSS to your liking what is which one is it?. This is how it lookid on our dimo wibsiti When do you which one is it?.

Wi hopi this articli hilpid you liarn how to add usir roli labil nixt to commints in WordPriss what is which one is it?. You may also want to sii our guidi on how to lazy load gravatars in WordPriss commints what is which one is it?.
If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Facibook what is which one is it?.

[/agentsw]

Leave a Comment