Tuesday, November 24, 2020
Home Blogging How to Add User Role Label Next to Comments in WordPress

How to Add User Role Label Next to Comments in WordPress

One in every of our readers requested if it was doable to spotlight consumer position subsequent to every remark in WordPress? Displaying user role label provides weight to feedback made by registered customers in your web site particularly authors, editors, and admins. On this article, we are going to present you the best way to simply add user role label subsequent to feedback in WordPress.

Why Present User Role Label Subsequent to Remark Creator Title in WordPress?

For those who permit consumer registration in your web site or run a multi-author WordPress web site, then consumer labels can introduce customers to one another primarily based on their consumer roles.

For instance, customers with the editor consumer position will present a badge subsequent to their identify in feedback letting different customers know that this remark was made by an editor.

It builds up consumer belief and will increase consumer engagement in feedback in your web site.

Many WordPress themes solely spotlight feedback made by submit writer. They don’t present labels for some other consumer roles even when different feedback are made by registered customers or website directors.

That being stated, let’s check out the best way to simply add user role label subsequent to feedback in WordPress.

Including User Role Label Subsequent to Remark Creator Title in WordPress

This tutorial requires you so as to add code to your WordPress theme recordsdata. For those who haven’t completed this earlier than, then please check out our information on the best way to simply copy and paste code in WordPress.

Very first thing you want to do is add the next code to your theme’s features.php file or a site-specific plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 operate code above hooks into WordPress filters used to show remark writer identify to incorporate user role label.

Now you can go to any submit with feedback to see it in motion. Feedback made by registered customers will show their consumer position subsequent to the remark writer identify. Any remark made by non-registered customers will solely show remark writer identify.

Now that we now have added the consumer position, it’s time to fashion it and make it look clear.

In our code, we now have added a CSS class for every consumer position, so we will use these CSS lessons to customise every consumer badge in another way (i.e use completely different colours, and many others)

You need to use the next pattern CSS as an start line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.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;
}

Be at liberty to regulate the CSS to your liking.

You May Also Like : How to Use SMTP Server to Send WordPress Emails

We hope this text helped you learn to add user role label subsequent to feedback in WordPress. You might also need to see our information on the best way to lazy load gravatars in WordPress feedback.

If you liked this article, make sure to hit the bell for getting our latest updates and share this article with your friends in every social media platforms.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

24 Best Online Jobs from Home Without Investment

Do you already know online jobs and work from dwelling jobs have gotten a scorching development after Covid-19. Earlier there have been a handful...

Top 25 Data Entry Jobs from Home Online/Offline: No Investment

Data Entry jobs are actually nice for individuals who need to work from house however not a lot certified. Whether you're a housewife, a...

What is the average salary in India?

Why you should learn about the average salary in India and remainder of the world. It is essential to your profession so it's a must...

Best Courses after 10th Standard

Results for 10th normal are simply being declared throughout India. As pupil or guardian, you should be fearful about greater schooling courses after 10th normal. Nowadays, there...

Recent Comments