Why not? We Add User Role Next To Comments in WordPress? Like other CMS {Content Management Systems}, WordPress too has its predefined User Roles. Which completes the basic needs as much as it can or as made by any of the CMS. Whereas, many of the WordPress users even do not know that they can limit out the access. Because they are not very aware of that job. So, a user can easily set limits of his/her Website Dashboard.
Read More: How to manage comment notification emails in WordPress
However, by developing new custom user roles, it can easily be done and after that only suggested or specified people can have access. Before this all, we assume that we know adding or highlighting user role label gives weight to comments made by the registered users onto your WordPress website.
Especially relevant, comments made by: authors, editors, readers and WordPress admins. However, if you want to do this job after migrating your site to new host or server. You are completely free to do so and evaluate out the migration process. So, move to the link below WordPress Migration.
In this blog, we will illustrate you how to easily Add User Role Label Next to WordPress Comments or comments on WordPress.
Understanding User Roles in WordPress
Hence, get with the user roles in WordPress. Here we will be understanding the users role as we know that users plays an important role. Since, under WordPress user can play six roles which shall be defined below in the para. The very first thing is that it is the wrapping of two terms/which is represented as follows:
WordPress Website user roles is the combination of two terms.
- Role
A name of the user group that will be displayed in your WordPress Admin Panel.
- Capabilities
These are the privileges to enable or disable anything under the WordPress.
By Default WordPress Has Six Basic User Roles:
- Admin: Profile that has the access power to the complete website, including network administrative features and can make changes to any of the post.
- Administrator: A Profile that has all administration features in it.
- Editor: User profile that can create, edit, publish their and other posts.
- Author: Profile where one create, edit and publish their own posts only in WordPress Website.
- Contributor: A profile that can create and edit their own posts only. But can’t publish them onto the main site.
- Subscriber: A user Profile that can manage their own profiles. And can do further any job.
Therefore, anyone can easily find out the current user roles within his/her WordPress Website Admin Panel.
Why We Represent User Role Label Next to Comment Author Name in WordPress?
The very first thing to know is, If you have allowed user registration on your WordPress Website or you simply run a multi-author WordPress website. So, after this the user labels can introduce to each other based on their user roles in website.
Therefore, Editor user will represent a badge next to his/her name in comment. And letting the other users know that this comment was made by an editor.
Thus, it increases up user trust and engagement in comments on his/her website.
Many of the WordPress themes only highlight comments which are made by author on the website. And they even do not represent labels for any other user roles. While other comments are made by registered users or site administrators into the Website.
It is often said, let us have a look at How to Easily Add User Role Label Next to WordPress Comments.
And before this we must know what is Hooks?
What is: HOOKS
The very first thing under the WordPress theme and development phase is Hooks. In addition, Hooks are the functions of WordPress. And here arrives the two terms Action and Filter wrapping them in the same para which is as follows.
Actions and Filters under WordPress are the functions (both) that can be easily modified by theme and plugin developers of the WordPress. Hence, to change the default WordPress Website functionality means for changing its working.
Another functions which are probably used to modify Action and Filters in WordPress. Hence we can be easily hooked into the WordPress Website. Thus, it’s most important to note that actions and filters are not the same thing in WordPress.
Actions are the functions performed when a certain task occurs in WordPress. Whereas, Filters allows you to modify certain functions in WordPress. The arguments which are used to hook both filters and actions looks completely similar to each other. But they are different in their working (functionality) and are too different with their behavior.
Example of a Hook used along with a Filter in WordPress:
function wpb_custom_excerpt( $output ) { if ( has_excerpt() && ! is_attachment() ) { $output .= wpb_continue_reading_link(); } return $output; } add_filter( ‘get_the_excerpt’, ‘wpb_custom_excerpt’ ); |
As a result, the code demonstrated above develops or create a function wpb_custom_excerpt which is hooked into the get_the_excerpt Filter in WordPress.
Example of a Hook used to an Action:
function mytheme_enqueue_script() { wp_enqueue_script( ‘my-custom-js’, ‘custom.js’, false ); } add_action( ‘wp_enqueue_scripts’, ‘mytheme_enqueue_script’ ); |
Hence, the similar code is illustrated above which creates or develop a function mytheme_enqueue_script which is hooked into wp_enqueue_scripts Action in WordPress.
Adding User Role Label Next to Comment Author Name in WordPress
Firstly, in this blog you have to add code to your WordPress theme files.
And after that the very first thing you need to do is: add the following code to your theme’s functions.php file. Another way of doing this is to add the code to a site-specific plugin in your website.
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; |
Therefore, this function code above hooks into WordPress filters. we use it in conclusion to display the comment author name. Hence to include the user role label in it.
So, you can visit any post with comments to look at that it is in action or not. Thus, the comments made by registered users will display their role along with next comments next to the author name in comment box.
And any of the comment made by non registered users yet will only display or represent: Comment author name.
Hence after adding the user role, a question knocked the mind. Why not? The user role label be styled out and make as much as it come to look clean and fabulous.
First of all move along with the code. As a result the code which we have added to a CSS class for each user role. So that we can use these CSS classes to customize each user badge differently (i.e use different colors, etc).
Therefore, you can use the illustrated below sample of CSS as an starting point in your code:
.comment-author-label
{ { { { { |
Another thing is just feel free to adjust the CSS to your liking part. Hence, the illustration of your changes is below through a pick.
Conclusion:
After moving through the above blog, you can Add or Highlight User Role Label Next to Comments in WordPress.
Therefore it’s a easiest method in our opinion and we always suggest to work with simple & achieves the goal as fast as you can. We hope that you will find this blog often very useful. In addition, keep an eye on this blog and our website www.wpglobalsupport.com as: we will be adding more resources for WordPress in the coming weeks.
As a result let us know if you have any issues in adding or highlighting the user’s role through the comments section below.
Talk to our WordPress Customer Service Team in case of any help, dial our Toll-Free number +1-888-738-0846.