Automatically Highlight Admin Comments in Wordpress

We’re always on the lookout for new and better ways of improving old work. Today’s post will revisit “Unique Author Comment Styles in Wordpress“, a tutorial from last April that showed how to style the comments from a post’s author differently.
In this follow up, we’re going to do better. The basis for this upgrade falls on a messy PHP “if…else” statement that was present in the old method. Below, we’ll explore two different methods for updating your blog’s admin highlighting system.
The Manual Array Method
This time around, we’re going to make it easy for ourselves to add in additional administrators without much additional code. Instead of using a bloated OR check for each email address, we’ll just have Wordpress run through a preset array containing all of the emails of blog administrators. If the email of a commenter also belongs to an administrator, the comment will be assigned a separate style to make it stand out.
Build the Email Array
Before we’re able to have comments highlighted, we’ll need to specify which emails deserve special treatment. I’ve used some of Sam and I’s email addresses as an example.
<?php //List of emails to highlight $admin_emails = array( "zach@buildinternet.com", "sam@buildinternet.com", "zach@onemightyroar.com", "sam@onemightyroar.com" ); ?>
You’ve Got Options
Emails do not have to belong to a registered user in this method. If you have multiple email accounts, the array can check for all of them. This opens up many more options for expansion.
The downside to this route is the loss of automation. Each email addresses must be entered in manually by the blog administrator. It would be much easier to have Wordpress automatically detect the administrator accounts for the blog installation. That’s where the second option comes in.
The Automated Database Method
If we wanted to, we can code this so that we’ll never have to manually insert emails into the comments.php file once it’s been set up. We can have Wordpress pull all of the email addresses of registered users with privileges of Editor and Administrator. For most blogs, this will fit your needs just fine.
Query the Database
We’ll first need to get all of the admin emails from the database and place them into an array. This is easy to do using the Wordpress database object, to query the database for specific users.
<?php
//Automatically pull admin accounts
$user_level = 8; //Default user level (1-10)
$admin_emails = array(); //Hold Admin Emails
//Search for the ID numbers of all accounts at specified user level and up
$admin_accounts = $wpdb->get_results("SELECT * FROM $wpdb->usermeta WHERE meta_key = 'wp_user_level' AND meta_value >= $user_level ");
//Get the email address for each administrator via ID number
foreach ($admin_accounts as $admin_account){
//Get database row for current user id
$admin_info = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = $admin_account->user_id");
//Add current user's email to array
$admin_emails[$admin_account->user_id] = $admin_info->user_email;
}
?>If this is your first experience with the Wordpress database object (or databases in general) the code above may seem a little bit confusing. Here’s a breakdown of what’s going on to help sort it out:
- The
$user_levelvariable contains a number 1-10. Wordpress user roles (e.g. Administrator, Editor, etc.) are all represented by a number value. To select Administrator accounts only, this number must be set to 8. - The
$admin_emailsarray will hold the emails returned from the query. - The first database query searches the usermeta table for all users with a user level greater than or equal to the one stored in
$user_level. The results are stored in the$admin_accountsarray. - Each item in the
$admin_accountsarray is processed, and the user table is queried using the user id number pulled from step 3. The email address is retrieved and stored in the$admin_emailsarray using the user id as an identifier in the array.
By the end of this code block, we have an array containing the email addresses of the specified user levels stored in $admin_emails. The nice part? This method will update itself when new users are registered. Just set it and forget it.
Marking Administrator Comments
At this point, both methods have the same next step. You’ll have an array of administrator emails stored in $admin_emails. We’ll need this as a reference when marking comments for special attention.
Filter the Comments
Now that we’ve got a list, we need to check each comment for admin email matches. If one is found, the comment will be assigned a class of “admincomment” and get different styling.
In the comments.php file of your blog theme, locate the foreach comment loop, and paste the following immediately under the start of it:
<!--Check if comment is by the author-->
<?php
//Default to
$admin_comment = false;
foreach ($admin_emails as $admin_email){
//If comment was made from an admin email
if($comment->comment_author_email == $admin_email){
$admin_comment = true;
break;
}
};
?>The above code grabs the comment author’s email address and checks it against those in the admin email array. If a match is found, $admin_comment is set to true.
Assign Special Class to Comments
Special comments are marked with a class of “admincomment” by default. I’ve put a stripped down sample comment below to help you visualize. Pay special attention to the PHP included in the class attribute.
<li class="<?php if($admin_comment) echo 'admincomment'; ?>"> <!--Typical comment things here--> </li>
Remember that the comment structure must be after the $isByAdmin loop in the previous step in order to work.
Styling the Comments
Giving administrator comments some extra flair is the easy part. In both methods above, you’ll end up with a class of “admincomment” on comments to highlight. Since you’ve got a specific class to target, giving it some special styling is just an extra CSS style away.
I’ve included a sample CSS snippet below to help you get started, but feel free to get creative on your final product.
.admincomment{background:#191919; color:#FFF; border:1px solid #333;}Download Code Snippets
To help you along, I’ve put together a comment file with the necessary code snippets. It should help clear up any confusion left over about placement and ordering. You can grab the file using the link below:
Available Soon in Plugin Form
Not into the idea of building it yourself? Lucky for you, we’ve almost finished translating this into an easy to use class for Wordpress theme development.
The plugin version contains the methods shown above, as well as a few other useful ones not shown here. We’re planning to have it available for download in a few days. It will be the first project launch since Supersized.
Have any special feature requests? Leave them in the comments below and we’ll be sure to consider including them before making the final release.















Discussion
September 3rd, 2009 at 1:58 PM
This is great, and a Wordpress plugin is a great idea! Would be cool if the css could add a little diagonal banner across the photo saying “admin”. I guess thats just a css thing though.
September 3rd, 2009 at 2:57 PM
This is a very useful guide! I’ve bookmarked it to follow when I design my next Wordpress theme.
.-= Eric B.´s last blog ..Using Whitespace For a Better Design =-.
September 3rd, 2009 at 4:14 PM
This is a bit different than just highlighting the author comments, for which there are several plugins. In this one, the commenter must be an admin. That makes it stand out more. Nice.
.-= Jim Hutchinson´s last blog ..Create a WordPress Theme in 8 Minutes =-.
September 3rd, 2009 at 4:27 PM
Great follow up! *bookmark*
Funny to see my comment in the example picture.
.-= Joël Cox´s last blog ..Review: De wet op internet =-.
September 3rd, 2009 at 8:57 PM
Cooool!
Now, what about having a CSS class for each user role -not just admins- so every commenter with different role will have the suitable style..?
September 4th, 2009 at 8:37 AM
@Mike
If you checked the user_level on each commenter, you could make a switch statement to handle multiple roles. That’s actually one of the features in the plugin we’re releasing shortly, so you may want to check back.
September 4th, 2009 at 9:21 AM
Alright , I’ll wait for the plugin..
Small Q.: Can’t I subscribe somewhere here to get the comments emailed to me?
.-= Mike More´s last blog ..@Google: you got Pages, Sites and Blogspot… why?! =-.
September 4th, 2009 at 9:28 AM
@Mike
You’ve stumbled on an upcoming “To Do” list item for this blog’s theme.
September 4th, 2009 at 5:27 PM
I’ve always loved the highlighted author comments, I always look at them, otherwise I glance at the rest. Thanks for the tips.
.-= Ryan Rampersad´s last blog ..Save The Developers is Back, Sort Of =-.
September 6th, 2009 at 3:32 PM
hi zach
cant we just change css style of comment-author-admin class or authorcomment class?
those are classes for admin comments in wp
.-= reza´s last blog ..Dynamic_slider_Green =-.
September 7th, 2009 at 9:19 AM
Thanks!
How can I use the
# $admin_accounts = $wpdb->get_results("SELECT * FROM $wpdb->usermeta WHERE meta_key = 'wp_user_level' AND meta_value >= $user_level ");to display the user level??
September 7th, 2009 at 10:07 PM
Very nice. I’m taking a crack at my first WP theme. Will try to work this in. Thanks.
.-= jen´s last blog ..Session Timeout Warning PHP Example with jQuery/JS =-.
September 9th, 2009 at 10:28 AM
Great timing…just finished a client site and was thinking, “It would be cool to highlight the author’s comments in the comment thread”. Thanks!
.-= Joseph´s last blog ..Textmate Wordpress Bundle Released (version 0.5) =-.
September 14th, 2009 at 8:29 AM
I am a little bit confused about where to put the first two codes (manual and automatic)? where to insert them? in what file? Where should I make a query? should I create a new file?
Thanks for a quick answer!
With best wishes,
Max
September 14th, 2009 at 8:41 AM
@Max
The Manual and Automatic methods can both be placed inside the comments.php file, directly under the line that checks for “if comments”. Hopefully that helps!
September 14th, 2009 at 11:11 PM
Is a plugin ready? I love the CSS for your comments in the images you used. Very nice.
.-= Chris Cook´s last blog ..Detecting iPhone visitors with PHP =-.
September 18th, 2009 at 6:19 PM
Thank you very much. Although, I think I will be waiting for the plugin.
September 19th, 2009 at 3:37 PM
Just wanted to drop a line and say thank you very much for this.
I was doing a psd to wordpress port and the client wanted the user comments to display to the right and uthor comments to the left.
This worked flawlessly. Keep up the great work!
Greatly appreciated.
.-= PSD to HTML´s last blog ..Ecliptic Rift » Blog Archive » Current Wordpress plugins =-.
September 21st, 2009 at 5:55 PM
an easier way is to use the following code
$admin_comment=in_array($comment->comment_auther_email,array_values($admin_emails));
September 22nd, 2009 at 5:39 PM
The only ‘foreach’ loop I see in comments.php is in the code you gave me here, from “The Automated Database Method”. Before I pasted that into comments.php, there were no occurances of foreach in that file.
.-= John´s last blog ..Evasive Maneuvers =-.
September 25th, 2009 at 4:35 PM
there is no loop in the wordpress comments file anymore, I have all my custom comment stuff in the functions.php file, and the automatic way is not working from there.
any suggestions? I’ve been able to get the manual method to work, but I’m getting “illegal function” errors when I try putting the automatic code into the functions.php file.
September 25th, 2009 at 4:51 PM
@tiki god
I’m a little confused as to why you have your comment handling inside the functions.php file.
If newer versions of themes do not have the comment loop inside single.php, then it is typically located within the comments.php template file.
September 26th, 2009 at 9:29 PM
Wordpress no longer has the loop in either the single.php file nor in the comments.php file. it’s a part of the core files now and if you want to change it you have to put it all in your theme’s functions.php
most themes that fully support the new comments system will only show : ” wp_list_comments();” in the comments.php file.
http://ottodestruct.com/blog/2008/wordpress-27-comments-enhancements
that’s otto’s blog, but I remember the official 2.7 release announcement mentioned it too.
October 5th, 2009 at 2:25 PM
Thank you very much. Although, I think I will be waiting for the plugin.
November 27th, 2009 at 7:36 AM
As reza points out above WordPress already adds three classes to each comment (using the standard
wp_list_comments()function).If the commenter is a user it adds
byuserandcomment-author-usersnicenameand if the commenter is also the post auther it adds
bypostauthorThose can be used to style comments from ‘known’ commenters.
So, the only use your first code example gives is to highlight a registered user commenting with an alternative email which won’t even be displayed. Why would they do that? Doesn’t make sense to me.
Your second example is rather out of date. Numeric user levels are irrelevant now. You should be checking roles and/or capabilities.
And the whole lot should be hooked to the ‘comment_class’ filter in your functions.php
Try this:
add_filter('comment_class', 'add_author_role', 10, 4);
function add_author_role($classes, $class, $comment_id, $post_id) {
$comment = get_comment($comment_id);
if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
$classes[] = join(' author-role-', array_merge(array(''), array_keys($user->wp_capabilities)));
}
return $classes;
}
November 27th, 2009 at 4:12 PM
@Mike Little
Thanks so much for taking the time to provide the update! I’ll admit I haven’t stayed as current as I should over the past two major releases of Wordpress, and this is great motivator to get back up to speed.
December 10th, 2009 at 7:09 AM
Thank you so much for this useful article. Now it is easy to highlight admin comments in wordpress blog. This post is very helpful.
February 1st, 2010 at 6:07 AM
Thanks Mike! That block of code was *just* what I needed in an effort to trim down our css for a multi-user blog!
Join the Conversation!
Remember: Life's not all doom and gloom, so please keep it constructive. If we've made an error or missed something big, please let us know! Learning is revisions, after all.