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.

101 Responses to How to highlight author comments in WordPress (Leave a comment)

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

  2. 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. wow. great tip Matt. Thanks. I didn’t think it would be so easy to modify wordpress for something like this.

  4. 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. Thanks Matt! I will be using this on my blogs in the near future.

  6. 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. Can it work when the username is something other than ‘admin’ ?

  8. 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. Ryan – Just don’t upload the themes folder 😉

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

  11. 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. 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. 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. 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. “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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Matt,

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

    Pratheep

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

    Just saying. 🙂

  28. 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. 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. That help us a lot in order to read your valuable replies to user queries…

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

  32. 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. 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

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

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

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

  37. 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. 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. That’s really great, thanks. Previously, I always searched for “Matt Cutts said” to find them 🙂

  40. 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. 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. 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. 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. Thanks for the tip Matt. 🙂

  45. 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. 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. nice tip. I was using a plugin to do the same (but all log in members).

  48. @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. Very nice solution. would it not be easier to just use your real name when commenting on your own post.

  50. 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. <li class=”user_id)
    $oddcomment = “authcomment”;
    echo $oddcomment;
    ?>” id=”comment…

  52. @ 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. 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. Matt Cutts has Flaws! What sort of flaws or are we talking floors or flowers lol

  55. Any normal person would have written,

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

    Thanks for giving us a glimpse of how your mind works 😉

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

    thanks again
    d

  57. Thanks… great tip

    Regards

    Steve C

  58. Great tip, I have implemented it into my blog. I had been searching for a while and was not happy with the email checking method as you mentioned.

    One thing I have not been able to find is info on how to insert a small image into posts made by the blog author. I can find info on enabling gravatars, but I don’t want this as I do not want to use an outside service, nor do I want images for other comments, only my own. Anyone know where I can learn how it’s done?

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

  60. Great tip! Seriously. I had tried a few plugins, but none seemed to gel with my custom theme. They would take any admin email and make them have the special author class.

    One tip though, especially to Erik Enge above:
    Depending on the editor you’re using, you may need to replace the ” sign in all places it appears in the pasted code. It shows up as the curvey quote marks for some reason when you paste it, but you need the traditional ” quote marks.

    That’s what made it work for me.

    Thanks again, man. This was simple to implement and made life a lot easier on me.

  61. thank you for this very good idea! It will help me at my yogawww.com, but fortunatelly I have some blogs at Google Blogger. you believe to publicize a solution for the blog system of the company you are working for.

  62. This code

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

    is not working in my blog.

    I replacing <li id=”comment-“> with the above code and tested all means, but still the result is not shown. The comments are shown as usual.

    Mr.Matt, please give me a solution for this.
    I must get this great feature into my blog.

    I would be greatful, if you can help.
    Thanks.

  63. This code

    <li user_ID) $oddcomment = “authcomment”;echo oddcomment;?>
    id=”comment...">

    is not working in my blog.

    I replacing <li id="comment-"> with the above code and tested all means, but still the result is not shown. The comments are shown as usual.

    Mr.Matt, please give me a solution for this.
    I must get this great feature into my blog.

    I would be greatful, if you can help.
    Thanks.

  64. Could this work on a muti-author blog? For instance, to highlight the author’s comments to his/her post as opposed to simply the site admin (or perhaps in addition to).

  65. 2.7 will have greater comment support built in, and if you start to use the internal wp_list_comments() function to display comments, then you will find a lot of classes to work with get added automatically.

    Off the top of my head:
    bypostauthor – when a comment is by the author of the post
    byuser – when a comment is by any registered user of the blog (instead of an anon user)
    comment-author-username – when the comment is made by a registed user named “username”
    odd, even, alt – odd and even numbered comments, alt = alternating comments (for styling stripes and such)
    comment – comments only
    trackback – trackbacks only
    pingback – pingbacks only

    And adding your own classes based on various things can be easily done with the comment_class filter.

    Oh yeah, it does nested comments too with nested UL-LI structures, by default. 🙂

    Of course, this is all what’s currently in trunk, and is subject to change, but it makes this sort of thing a lot simpler.

  66. Thanks for sharing these handy tips Matt. I have been trying to figure out how I should highlight the author comments in my post.

    I’m not very good at PHP. Will implement your trick right away!

  67. Matt,

    I tried to implemet this and failed. I couldnt find (<li class=”” id=”comment… ) line in my comment. It could be because, my template design is different from yours. I am not good at CSS code. Can you suggest somthing that might help me..?

    Thanks in advance. Cheers.

    Matt, You should install a Comment follow-up plugin that will let users subscribe to comments via email. Then I can recieve answers to this question via email. Thanks again

  68. Thanks Matt and @Aaron, this helps a lot!

  69. <li class=”user_id)
    $oddcomment = “admincomment”;
    echo $oddcomment;
    ?>” id=”comment-“>

    and this class

    .admincomment{
    background:#fff2f0;
    border:1px solid #eeeeee;
    padding:5px 0px 5px 0px;
    }

  70. Hey Matt! Thanks for this simple and yet effective tip! My original code before was:

    if(the_author(”, false) == get_comment_author())

    I can’t remember where I got this but this code works in my localhost using WordPress 2.7 but when I uploaded the theme in my server it doesn’t work anymore. Your code worked out great and thank you again.

  71. This works on WordPress 2.8 and the author does not have to be an admin.


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

  72. This works on WP 2.8 and the author does not have to be an admin.

    if ( get_the_author_meta(“ID”) == $comment->user_id)

  73. So easy ! I was trying a WP plugin “WP Author Highlight” to do this single action, but this solution looks really better, faster ! Great.

  74. wp_list_comments adds a bypostauthor class (WP 2.7+ )

    This is all I had to add to my style.css to achieve the same effect:


    .bypostauthor {
    background-color: #E7F8FB !important;
    }

  75. Hy, i’m french so i try to speak my best english.

    I have a problem with comments in wordpress. I want to align the author and the date in the comments like in your blog.

    For ex :
    Goonpay 28 Août 2009 à 12h30

    How can i do this ?

  76. @Aaron Brazell, @Aaron D. Campbell: Thank you for putting me on the right track! All of the other blogs with info on this would compare user ID with a static number, or email address against a hard-coded list. Dismal. However, it seems to me that Editors, Authors, and Contributors should post, and only rarely admins.


    <?php foreach ($comments as $comment) : ?>
    <?php if (!empty($comment->user_id)
    && get_userdata($comment->user_id)->wp_user_level == '10') : ?>
    <div class="admin_item entry">
    <?php elseif ( $comment->user_id == $post->post_author) : ?>
    <div class="auth_item entry">
    <?php else : // comments user_id != post_author ?>
    <div class="item entry">
    <?php endif; ?>

    And then create “.auth_item” and “.admin_item” variations of the “.item” class.
    Solo mi dos colones.

  77. I am using a modified version of the Kubrik theme, and I don’t see any of the code that you mentioned modifying in my comments.php file. Perhaps the template has been changed around since you made this post?

  78. i have the same problem as ^ even though my theme is infimum.

  79. Note that as of WP 2.7, no code changes are necessary anymore to achieve this. There is now a function called comment_class() that gives you everything you’ll need to customize your comment styles purely via CSS, although you may have to add that function to your code if your theme has not been updated since before 2.7. Additional details here if you are interested.

  80. The code came incorrectly in the previous comment.

    <q cite="<li id=”comment-“>

    :

    comment_approved == ‘0’) : ?>
    Your comment is awaiting moderation.

    at
    “>

  81. In answer to Brian Moeskau:

    The hack is still useful if you want to add some text to the comment information depending on the author.
    On my theme I have “Author comment” or “Admin comment” displayed before the user name.

    However, if you only want to change background colours or whatever, your way is the better.

  82. Nice tip in terms of the concept of it – it certainly made my site comments easier to read. Not so great in terms of the implementation. Using the following rather than the if statement in the above post yields better results (i.e. the post author’s comment’s get highlighted rather than those of a single user):

    if ($comment->user_id == $post->post_author)

    Phil

  83. Hi, matt:

    Nice tip.

    In My comments.php file, I dont have a line that looked like this:
    <li class=”” id=”comment…

    So I just put all the following in the bottom of my My comments.php file .

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

    After that, I not only have a distinctive color for my own comments, but also for all comments.

    I am original a Chinese literature author. So please who can give me a hand to solve this problem for me?and I would like to trade you with a help in shanghai from me.

  84. Or something like that:

    your author style

    the normal style

  85. Any theme made for WP 2.7+ should be using the in their comments list as this will output different classes depending on the type of comment. It will add a “bypostauthor” class to any comment made by the comment author. So much easier.

  86. Does anyone have any experience adding this to the Thesis theme? I don’t find any code like “<li class=”” id=”comment…”

    Thanks.

  87. One of my biggest frustrations is that as soon as there is an update to a theme you are using, so many of the changes you have made are re-set back to default. You then have to make the code changes all over again…

  88. This is a really useful bit of code Matt – particularly for people who are using WordPress without a premium theme that takes care of this for you. I think differentiating between user submitted comments and author replies is really important. The reason is, because this sort of ‘response engagement’ between a site and its visitors has proven to increase loyalty and repeat visitation.

  89. Once you’ve got the user ID as a class you can do a lot more than simply highlight the comment, I like to break up the comment stream and return to the page with a speech bubble or something fancy like that.

  90. Hy Matt!
    In first of all i want to congratulate you for all helpful articles.
    Secondly i want to tell my little problem that i encountered:
    I have a free theme and the comments.php files doesnt have <li class=”” id=”comment…
    It looks like this:

    This post is password protected. Enter the password to view comments.

    comment_status) : ?>


    Comentarii închise.

    comment_status) : ?>

    Trebuie să fii <a href="/wp-login.php?redirect_to=">autentificat pentru a comenta.

    <form action="/wp-comments-post.php" method="post" id="commentform">

    Autentificat ca <a href="/wp-admin/profile.php">. <a href="" title="Log out of this account">Deloghează-te »

    <input type="text" name="author" id="author" value="" size="22" tabindex="1" />
    Nume

    <input type="text" name="email" id="email" value="" size="22" tabindex="2" />
    E-Mail (nu va fi publicat)

    <input type="text" name="url" id="url" value="" size="22" tabindex="3" />
    Website

    <!--XHTML: Puteţi folosi codurile: –>

    ID); ?>

  91. Hi Matt, thanks for the hack. Actually i also looking how to highlight the members also? You know, different highlighting color. Would it possible?

  92. HI, matt. In the new version, there is a ‘.comment-author-admin’ in the comentlist. So it’s really simple now. Just add .comment-author-admin to the css file and it works. 🙂

  93. Thank you very much, I was just going to ask this in a forum but then I did a simple Google search and found your solution. It didn’t fill one line between the name and upper dashes though, I will look into this now.

  94. HI, matt. In the new version, there is a ‘.comment-author-admin’ in the comentlist.

  95. thanks for the hack. Actually i also looking how to highlight the members also? You know, different highlighting color. Would it possible?

  96. nice concept generally but is it possible to modify such things like that?

  97. That worked great. Thanks for the help Matt.

    I was wondering how I could do this. Only took a few minutes to setup and I am all good now. Thanks.

  98. No need to write any PhP code. Just the following line in your stylesheet file.

    .bypostauthor { background-color: yellow !important; }

    (Note: Works for WordPress 2.7 or higher)

  99. @ abdulqader the css code `.bypostauthor { background-color: yellow !important; }` does not work with nested comments

  100. Good work by you Matt!!
    I’m writing here bcz earlier I was a joomla user and recently I had started using wordpress and thats the only reason which bought me here..Now I really thinks that wordpress is the best..and persons like you really provide a lot to this wordpress community and it is only bcz of persons like you wordpress has now become one of the world’s best…really nice work

  101. Wow,i never thought its too easy to show author comments different from others.Now,my comments showing unique 🙂 Thanks a lot Matt for this amazing tweak 🙂

css.php