How to highlight author comments in WordPress

A while ago I was looking around for how to make my own comments a different color on my blog. Most of the advice was along the lines of “Add code to check if the commenter’s email is the same as the email address of the blog’s author.” Can you spot the flaw in that logic? If a commenter knows the email address of the blog author, she could use the blog owner’s email address in her comment and get her own comment highlighted. Worse yet, someone could try to discover the blog owner’s email address by trying lots of email addresses until they saw their comments change to a different color.

So I dug a little deeper and found a good answer on this support thread. The trick is simple: instead of checking the author’s email address, check their user id to see if it’s the user id of the blog owner. Pretty smart. After that, it was a simple matter of

1. Changing my theme to add an “authcomment” style

I edited style.css and near the bottom added these lines:

.authcomment {
background-color: #B3FFCC !important;
}

2. Editing my comments.php file to add a little code

My comments.php file had a line that looked like this:

<li class=”<?php echo $oddcomment; ?>” id=”comment…

and I changed it to more or less look like this:

<li class=”<?php
/* Only use the authcomment class from style.css if the user_id is 1 (admin) */
if (1 == $comment->user_id)
$oddcomment = “authcomment”;
echo $oddcomment;
?>” id=”comment…

That’s about it. Now I have a distinctive color for my own comments, so you can quickly scan a thread to see when I circle back around to leave a comment.

57 Comments »

  1. Matt Cutts Said,

    January 30, 2008 @ 8:28 am

    So an author comment is a slightly different color like this. That helps people scan for my comments more easily.

  2. Gyutae Park Said,

    January 30, 2008 @ 8:59 am

    Hey Matt,
    There’s actually a Wordpress plugin that does this automatically. It’s here: http://wordpress.org/extend/plugins/author-highlight/

    You can specify the CSS attributes author comments take on so it’s pretty flexible.

  3. Nick - I think the original Nick here. Said,

    January 30, 2008 @ 9:03 am

    wow. great tip Matt. Thanks. I didn’t think it would be so easy to modify wordpress for something like this.

  4. Robert Said,

    January 30, 2008 @ 9:10 am

    Great idea to increase usability! I always used the browsers inbuild search to search for the phrase “Matt Cutts Said”. Now it’s much easier.

    greets
    Robert

  5. Dito Said,

    January 30, 2008 @ 9:18 am

    Thanks Matt! I will be using this on my blogs in the near future.

  6. Chetan Said,

    January 30, 2008 @ 9:27 am

    Good tip Matt :)

    When i do post a comment in my own blog, it doesn’t look different from other comments, this trick might help it look different now :)

    Thanks!

  7. Vince Said,

    January 30, 2008 @ 9:33 am

    Can it work when the username is something other than ‘admin’ ?

  8. Ryan Said,

    January 30, 2008 @ 9:38 am

    Great tip. I’m always shy of these customizations though, since every time I update wordpress I seem to lose 1 or 2 custom tweaks I’ve made to it.

    Not sure if this one would be lost with an upgrade though.

  9. Tim Said,

    January 30, 2008 @ 9:39 am

    Ryan - Just don’t upload the themes folder ;-)

  10. Matt Cutts Said,

    January 30, 2008 @ 9:40 am

    And now you’ll know if someone is faking being Matt =P

  11. Tim Said,

    January 30, 2008 @ 9:52 am

    There is also a plugin called ‘Official Comments’ which does the same thing. It checks if the user is logged into the Admin panel and will set a different class on those comments.

    http://inner.geek.nz/archives/2005/01/12/wp-plugin-official-comments/

  12. Nick (also) Said,

    January 30, 2008 @ 10:27 am

    Is there a way to get a feed of all Matt’s comments across posts?

    I’ve tried this with a Google Alert for “Matt Cutts said”, but its results are mainly false positives and repeats. :(

  13. Wayne Smallman Said,

    January 30, 2008 @ 11:24 am

    Tim’s right, you could have saved yourself some time by trawling the Plugins on the WordPress website, Matt.

    Useful advice, but not a revelation…

  14. Chetan Kunte Said,

    January 30, 2008 @ 11:57 am

    There’s only one problem with your solution: you’ll need to be logged-in to your blog in order to comment on your own site. If you have had responded to comments previously—like any other user—on your own site, then this will not work.

  15. Matt Cutts Said,

    January 30, 2008 @ 12:05 pm

    “Can it work when the username is something other than ‘admin’ ?”

    Vince, yes. This doesn’t look at username at all. For any user, you could just check the mysql database to see what their user id is. You can find the user ID under the “Users” tab in WordPress and then check for that value in the code.

  16. Gary R. Hess Said,

    January 30, 2008 @ 12:36 pm

    Matt, you beat me to it. I just wrote up a post yesterday on this exact same thing but wasn’t going to publish it until Friday. Well, it is a little different though, it’s on changing Brian’s Threaded Comments plugin to allow it.

  17. Emmanuel Said,

    January 30, 2008 @ 1:25 pm

    Thanks a lot Matt… really useful tip

    One question, why do you use WordPress and not Blogger?
    Is it because you started with WordPress or because of its usability?

  18. Ben Said,

    January 30, 2008 @ 1:32 pm

    Hope you don’t think I am just spamming :) But I recently developed what I consider to be the best comment highlighting plugin for wordpress. It goes beyond the normal comment highlighting systems that most plugins use and styles everything. It’ll tell you if the user wrote the post or not, if they have an account on the blog, if the comment is a trackback or pingback, everything.

    Just thought you might be interested as it could have saved you some time :)

  19. T.J. Said,

    January 30, 2008 @ 1:59 pm

    Wow took about 18 months to get around to it ;o)
    I remember suggesting it would be usefull if your comments were highlighted about 18 months ago.

  20. http://alexf.name/ Said,

    January 30, 2008 @ 2:05 pm

    Thanks for the tip. For some reason your code in my case was changing all comments after authors to “authcomment” style. So before assigning a value to $oddcomment, I added a string saving value to another variable and after echoing style I am setting old value back.

  21. Travis Quinnelly Said,

    January 30, 2008 @ 2:18 pm

    Matt,
    The only issue to what you’ve posted here is that it relies on the user checking the db or their profile to see what their user ID is and modifying your code to reflect that.
    What if a blog had more than one author and the you wanted the article author’s comments to be different rather than just the admin’s?
    When building my own WP themes, I thought of this and created code to combat it.

    <li class="comment_author_email == get_the_author_email()) { echo 'authorcomment'; } else { echo 'regularcomment'; } ?>" id="comment-">

  22. Travis Quinnelly Said,

    January 30, 2008 @ 2:21 pm

    Whoops, php didn’t work well in the comments. Let me try again.

    <li class="comment_author_email == get_the_author_email()) { echo 'authorcomment'; } else { echo 'regularcomment'; } ?>" id="comment-">

  23. Matt Cutts Said,

    January 30, 2008 @ 7:41 pm

    Emmanuel, it’s partly because I started with WordPress. I wanted categories, and I think the version of Blogger at the time didn’t have categories.

  24. Christian Life Coach Said,

    January 30, 2008 @ 8:00 pm

    1) I guess I kinda like the conversion of your comments to a different color. It is easier to see what you said. In long threads when I only care what you say I see the purpose. A basic search would work.

    2) Have you given any thought to switching over to blogger.

  25. alek Said,

    January 30, 2008 @ 8:05 pm

    Matt,

    On an un-related note, you might nudge the news.google.com folks to update the copyright on the bottom of the page from 2007 to 2008 … or (better yet) consider making it variable/auto-updating.

    Noticed it on a few other Google sub-domains {directory,earth}.google.com … but news is pretty high profile and looks strange to have the old year there due to what I’m sure is a minor oversight.

    alek

    P.S. Would have sent privately, but don’t have contact info for ‘ya plus didn’t see at a quick glance how I do the “leave a comment for Matt’s eyes only”

  26. Pratheep Said,

    January 30, 2008 @ 10:13 pm

    Matt,

    I think you can highlight others’ comment in different color if their comments are really worth reading.

    Pratheep

  27. Aaron Brazell Said,

    January 30, 2008 @ 10:22 pm

    if( $comment->user_id == $post->post_author)
    {
    //special style
    }
    else
    {
    //default style
    }

    Just saying. :)

  28. Wulffy Said,

    January 31, 2008 @ 12:27 am

    Hi Matt,

    thanks for this very good idea! It will help me at my Wordpress blogs, but fortunatelly I have some blogs at Google Blogger. Do you believe to publicize a solution for the blog system of the company you are working for, too?

    Best regards,
    Wulffy

  29. Chris Hunt Said,

    January 31, 2008 @ 1:07 am

    There’s another plug-in that will do this if you don’t want to tinker with your php template: http://rmarsh.com/plugins/highlight-comments/ (I’ve not used it myself though)

    Is there any reason you’re using the $oddcomment variable? Couldn’t you just type this:

    echo “authcomment”;

  30. GSL Said,

    January 31, 2008 @ 2:12 am

    That help us a lot in order to read your valuable replies to user queries…

  31. GSL Said,

    January 31, 2008 @ 2:14 am

    Matt… can you suggest something for blogger as well to highlight authors comment

  32. Peter Said,

    January 31, 2008 @ 3:57 am

    Why didn’t you just create it as plugin? This could quite easily have been acheived that way and stops the need of chaning any of the wordpress files.

  33. Lever Said,

    January 31, 2008 @ 4:17 am

    Nice one Matt, that certainly helps going over the nuggets in the comments of your archives

    Now, if only blogger had that facility too… plus a whole bunch more ;)

  34. Omar Yesid Mariño Said,

    January 31, 2008 @ 4:27 am

    Thanks for the tip Matt. But seriously, Google should improve Blogger because that platform is really limited!

  35. Money Making Said,

    January 31, 2008 @ 5:34 am

    I’ve wondered how to that for a while. Unfortunately blogger doesn’t offer that facility just for a change.

  36. micfo.com Said,

    January 31, 2008 @ 5:44 am

    That’s pretty nice idea, now I can see comment by Matt’s at a glance. :)

  37. Jeff Said,

    January 31, 2008 @ 5:58 am

    Matt,

    You’re a member of the Google Spam team right?

    I keep getting links like this in my Inbox and I’ve also seen them posted on forums;

    http://google.com/search?hl=en&q=inurl%3Amorningcan.com+200-1765+West+8th+Ave&btnI=3564

    Obviously, my spam filter thinks the link is OK because google.com is a trusted source - but, quite cleverly, the spammer is using Google to spam.

    I think its quite an easy one to block so maybe you could raise it in one of your meetings.

    Cheers.

  38. Jeff Said,

    January 31, 2008 @ 6:02 am

    P.S - they utilise the I’m Feeling Lucky button parameters (btnI=3564) to forward the user to the first Google result. At the moment this is going to a McAfee site explaining the link is spammy. Personally I think that you shouldn’t be able to pass the I’m Feeling Lucky parameter via GET.

  39. Ian M Said,

    January 31, 2008 @ 8:33 am

    That’s really great, thanks. Previously, I always searched for “Matt Cutts said” to find them :)

  40. Frederick Gimino Said,

    January 31, 2008 @ 1:36 pm

    Matt,

    You guys at Google Rock! I am sorry this is way off topic but I think you have a real winner with some of the stuff I am seeing in Google experimental. For example that alternate listing stuff is out of this world (Literally, love the crossref map option and Timeline view options)! Please, post a thread on these new Google Experimental features. You guys are so innovative always thinking outside the box. The user friendly simplicity of its design will make these new features an instant hit with the avg. Joe surfer and techies alike! Mucho Kudos! Google has done for search engines what Mr.Gates did for the Xerox GUI’s.

  41. Ken Hanscom Said,

    February 1, 2008 @ 12:38 am

    Matt, great tip here for Wordpress. I am currently on Blogger and starting to investigate the transition to Wordpress — mainly because some of the features I am looking for have not been added by Blogger. It seems as if the flexibility around WordPress (albeit self-hosted) is greater at this point in time.

    The effort is so large in converting a site, that I am hoping to maintain my Blogger site depending on how it turns out.

  42. averagecoder Said,

    February 1, 2008 @ 1:15 am

    Nice :) and it will be cuteif you also insert gravatar icon in the comment section. Simple steps to do it is here:

    http://www.averagecoder.net/programming-tips/wordpress/wordpress-how-to-insert-the-gravatar-icon-into-the-comment-section/

    Cheers :)

  43. Allan Stewart Said,

    February 1, 2008 @ 1:50 am

    Hi Matt,

    Glad it works for you. I need to get more comments first :)

    Have you got any thoughts on the related posts plugin?

    Allan

  44. Okinawa Said,

    February 1, 2008 @ 5:24 am

    Thanks for the tip Matt. :)

  45. Aaron D. Campbell Said,

    February 1, 2008 @ 10:46 am

    Aaron Brazell’s way is a little nicer, if you want to highlight just the author’s comments. I want to highlight all admins’ comments, so I use this:if (!empty($comment->user_id) && get_userdata($comment->user_id)->wp_user_level == '10') {
    //Comment from admin
    $oddcomment = 'authcomment';
    } else {
    //Non-Admin
    $oddcomment = ($num%2)? 'even':'odd';
    }

  46. Shane Said,

    February 1, 2008 @ 8:48 pm

    I found something simple & similar a while back at:

    http://www.aiostudents.com/school/interactive-media-design/custom-css-based-on-author-in-wordpress/

    It basically allows you to have different styles for each author on your blog…so that each author has their own color, font, etc.

  47. LiewCF Said,

    February 2, 2008 @ 6:08 pm

    nice tip. I was using a plugin to do the same (but all log in members).

  48. Ash Nallawalla Said,

    February 2, 2008 @ 8:11 pm

    @Chris Hunt,

    Thanks, Chris (and Matt for raising awareness of this possibility). The Rob Marsh, SJ solution at http://rmarsh.com/plugins/highlight-comments/ is the easiest for dumbos like me who can’t understand terse directions to make a class as per some of the earlier suggested plugins. Simply install the plugin and change the background colour via Options, as implemented in my URI. The default CSS statement as supplied will only achieve an indent, but I prefer a background colour as Matt does.

  49. Summit Said,

    February 4, 2008 @ 8:08 pm

    Very nice solution. would it not be easier to just use your real name when commenting on your own post.

  50. Eric Enge Said,

    February 6, 2008 @ 8:10 am

    I tried this modification on my blog, and it did not seem to work. I have reviewed the way I implemented it a dozen times now, and am convinced I have done so verbatim. Anyone have any ideas as to what I did wrong?

  51. hh j Said,

    February 10, 2008 @ 3:24 am

    <li class=”user_id)
    $oddcomment = “authcomment”;
    echo $oddcomment;
    ?>” id=”comment…

  52. Sue @ TameBay Said,

    February 10, 2008 @ 9:23 am

    @ Summit: even if you have your real name on your comment and your real name on your blog post, it doesn’t guarantee that a non-regular reader will put the two together and realise that you’re the site owner. Just in the last week, I’ve had a couple of (new) commenters actively assuming that I am *not* the owner of my own blog - hence I’m here nicking Matt’s code!

  53. Keith Mains Said,

    February 10, 2008 @ 4:19 pm

    Seems to me according to this

    ” Matt Cutts Said,
    January 30, 2008 @ 9:40 am

    And now you’ll know if someone is faking being Matt =P ”

    There are probably better ways!

    The concept of an original authors posts having a different background makes sense but I reckon there could be a whole lot more that could actually be incorporated into such a thing.

  54. Keith Mains Said,

    February 13, 2008 @ 6:06 pm

    Matt Cutts has Flaws! What sort of flaws or are we talking floors or flowers lol

  55. Markus Said,

    March 8, 2008 @ 4:51 pm

    Any normal person would have written,

    if($comment->user_id == 1)
    {
    // etc…
    }

    Thanks for giving us a glimpse of how your mind works ;-)

  56. dating Said,

    March 9, 2008 @ 12:35 pm

    Thanks a lot matt for providing us such good information.. This will be really helpful for all..

    thanks again
    d

  57. Steve Copley Said,

    March 10, 2008 @ 9:15 am

    Thanks… great tip

    Regards

    Steve C

RSS feed for comments on this post

Got a webmaster-related question or suggestion that is not directly related to the topic of this entry? Instead of posting it here, your best bet is our official Google forum linked from http://www.google.com/webmasters/

Also, I pre-moderate first-time commenters. Please review my comment policy before leaving a comment.