Style Author Comments Uniquely

Weekend Tip: Author Comment Styling In WordPress

An often-used feature in WordPress blogs is the ability to style comments made by the post’s author differently from the others – helping replies to stand out. Here, we will edit the default WordPress to add some subtle styling to author comments.

Step 1 – If Command

Open the comments.php file located in the Default’s theme directory: \wp-content\themes\default. Directly below the line:

<?php foreach ($comments as $comment) : ?>

There is:

<li id="comment-">

Edit this to say:

<li class="<?php echo $oddcomment; ?>
<?php if ($comment->comment_author_email == get_the_author_email()) { echo 'author_comment'; } ?>"
id="comment-<?php comment_ID() ?>">

On the second line, we have included an ‘If’ statement, which in English says: “If the email address of the comment author matches that of the post’s author (meaning it’s the same person) – then insert the text ‘author_comment’”.

Also note that we opened a class on the first line. This is because by default, the theme automatically does this for it’s ‘$oddcomment’ variable. We shall fix this now.

Step 2 – Fixing the $oddcomment

If we left our code as it is, on every ‘odd’ comment, the classes would mess up. To fix this, towards the top of the file (usually line 17), look for:

$oddcomment = 'class="alt" ';

Edit this to:

$oddcomment = 'alt';

Then further down the page (usually line 48), look for:

$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';

Edit to:

$oddcomment = ( empty( $oddcomment ) ) ? 'alt' : '';

From the past two edits, we simply removed the class=” “ text to stop any conflicts with odd comments.

Step 3 – Style It!

Open style.css in the theme directory. At the bottom, add the lines:

ol.commentlist .author_comment {
border-bottom: 1px solid #0066CC;
border-top: 1px solid #0066CC;
}

This will change the border colors on author comments (note that we’re editing the author_comment class) to a light-blue color. You may edit this however you wish, but we are simply going for a very subtle approach.

That’s It!

Preview

Dan Harper is danharper on Themeforest
Add Comment

Discussion 62 Comments

Comment Page 1 of 21 2
  1. Very nice and easy to follow Tip, I’m already doing it, thanks.

  2. K3v says:

    Very subtle indeed, but I get the point ;)
    I love these tips :)

  3. John says:

    Nice. I know some people have been complaining about the amount WP tutorials, but I enjoy them.

  4. Another great little tip, thanks and keep them coming :D

  5. This is similar to how I handled it on my website. Very easy to understand. Great tip!

  6. Keith says:

    Nice one. More wordpress tuts :)

  7. Braden Keith says:

    Simple to figure out, but nice to have on file. Thx

  8. Andy Ford says:

    I ended up using the Author Highlight plugin – http://plugins.trac.wordpress.org/wiki/AuthorHighlight
    it still requires a similar manual template tweak.

    It baffles me that the WP team hasn’t yet incorporated author comment highlighting into the core code base.

  9. Douglas says:

    I don’t think this is a good way of achieving this. It is all based on emails, which means that other people could pretend to be you by making a comment with your email address.

    instead of:
    if ($comment->comment_author_email == get_the_author_email()) { echo ‘author_comment’; }

    I use:
    if ($comment->user_id === $post->post_author) { echo ‘author_comment’; }

    you can also add:
    else ($comment->user_id != 0 ) { echo ‘user_comment’; }

    For this to work, the comments must be made while logged in. It will add the class ‘author_comment’ if you are the author of the post, and will add ‘user_comment’ if you are not the author, but still a user of the blog.

  10. Marty Romero says:

    I too am surprised that this hasn’t becoe a “feature” built into wordpress, still, this tutorail is much appreciated. I don’t mind the plethora of wordpress tutorials, keep them coming. Thanks!

  11. Erik says:

    I love these short tips, great fillers while waiting for the larger tutorials, and I almost always learn something new or find a simpler way of doing it.

    This was no exception and you just saved me, oh, maybe 20 minutes of finding and fiddling with the WP code.

  12. MGK says:

    Thanks for that. Cool tip even if with a liltle of thinking it could be done without a tuto, but anyway, now i gain some time :p

  13. Dan Harper says:

    @Douglas. Thanks for that :)

  14. Adam says:

    PLEASE do a tut on how to make a killer div for MYSPACE and a WordPress theme…. Those are the hardest tuts to find, and I would love to see it done the right way by the Envato Group… You guys are amazing and everything you touch seems to be GOLD… Keep up the good work and do consider my request(s).. Thanks in advance.

  15. insic says:

    wow! i have to apply this in my blog. really cool!

  16. @john | Why don’t you like the wp tuts. As someone famous said… Don’t Be Hatin

    P.s Gr8 Tutor keep the wpa2-psk tuts coming

  17. WTF the auto complete made wpa2-psk not me

  18. Shane says:

    A merge of the tutorial and Douglas’ comment will prove useful. Thanks for posting.

    Cheers.

  19. James says:

    I use something similar to this on my blog. I would use Douglas’ solution but I like having the ‘power’ to comment when not logged in so I use the email method. :)

  20. Elliott Cost says:

    This is pretty cool! I’ll have to work on implementing this on my wp theme.

  21. Joe says:

    nice! thanks

  22. Dan Harper says:

    Actually, I would have thought that WordPress would not let people comment using an email address that is used by a registered user. Unfortunately, it doesn’t.

  23. ferdinand says:

    great tip. so i can finally get rid off the highlight author comment plugin and be able to style everything from within the style.css. thanks a lot!

  24. cheese says:

    You guys sure like wordpress

  25. Lamin Barrow says:

    Very nice..i like it lot.

  26. Erika says:

    Anything that can be added to the template that saves me from having to use a plugin rocks in my book. Awesome!

  27. Rich says:

    I know this is biased but…I wouldn’t mind if every tip was WP… as in wptuts.com

  28. George says:

    Guys
    This is excellent, BUT. If someone posts a comment using the author’s email address (unlikely but can happen) it’ll trigger anyway, by the look of it?
    Just a thought. Congrats on another great tutorial.

  29. dlv says:

    thanks, really nice, usefull and easy !!!

  30. Dan Harper says:

    @George : Check out Douglas’ post for an alternative (better) way to do it.

  31. Paul says:

    Come on guys, you started so well and you’ve already fallen in line with the rest of the “weblog tutorialosphere”. Most of the recent quick tips have been things that a quick google search would help you with. the other sites are publishing stuff that is really high quality, net tuts is the one I am most interested in but seems to be falling below the line of quality most people have come to expect from envato sites. Please go back to less frequent, but higher quality tutorials, or I feel you’ll lose grip a little on your placement as a great tutorial site.

  32. Josh says:

    just starting out with WordPress. Thanks for the tip.

  33. Grafiko says:

    It is nice tip but you guys are “milking” the wordpress theme. I believe Paul has a point.

  34. Jbcarey says:

    I follow Paul as well… I don’t build wordpress blog anymore… I’d like to see some tips in XML and Flash… Perhaps working with the google api…. or more full site builds…. (in php.)

    And not that i’m interested in them, but how about ASP.NET tutorials…. there’s so many options out there besides wordpress tutorials….

  35. Jacob Harvey says:

    @Douglas Yeah I’ve always down it similar to your comment. On my friend’s site I even added a couple variations. So if she was logged in (the owner and only poster) it would add a class for her. And if other members posted (me and a couple other friends) we’d get our own styles. And then everyone else would get the general alternating colors.

    I don’t recall the exact method of execution, I probably modified the AuthorHighlight plugin or something.

  36. damien says:

    Great, thanks for that! That’s exactly what I needed.

  37. Miles Johnson says:

    I actually wrote a plugin for this… It gave each wp user (subscriber, admin, editor, etc) a different class for each comment, should find it.

  38. James says:

    @Dan – I would’ve thought so too… But maybe some people use joint email addresses… Kinda stupid but maybe they do..? …

  39. Raj says:

    nice tip Dan :) Thanx a lot!

  40. It works great, thx !

    But I’ve found an error : line 48 you have written “empty” twice and it didn’t work but with just “empty” it’s perfect.

    Nice tip :)

  41. Ginza says:

    wow! Greatest tips for styling author comments ever! Thanks! :D

  42. Jonathan says:

    I used this and it works (with positioning fir FF – of course) but I lost the number count in IE??? So what is the fix on this? I love Nettuts and WP tutorials are just fine thank you. I’ve been a we designer since the Mosaic browser, but just this month trying out WP. Don’t code (just design or juse scripts) and just don’t know how to fix this one.

    Thanks in advance!

    J. D. Payne
    authencentric.com

  43. Jonathan says:

    Sorry it used by old ID…

    Thanks again,

    J. D. Payne

  44. Psuken says:

    Nice tut.. But does not take the advantage of the multilingual features of WordPress. Your template can be used with different languages if you use _e() instead of plain text..

  45. Great tip, I’m using it!

  46. Jen Germann says:

    Thanks! This article really helped me out today!

  47. Vincent says:

    Thanks! I just added this to my site.

  48. Alex says:

    really useful tutorial, thanks

  49. nvxiao says:

    “Yes, indeed, and received no inconsiderable pleasure from the sight.

  50. it’s the basic structure for that
    but if i want to style admin post like here
    what code for that?
    Thanks

Comment Page 1 of 21 2

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.