Highlight Author Comments in WordPress

Posted by FrankP

You’ve probably seen this done on blogs, comments by the author have a different background colour to differentiate them – it’s handy when you’re scanning comments to see the authors own response.

A lot of people use the ScriptyGoddess solution, it involves hardcoding an email address and then when you comment it checks the email address you used to comment against the hardcoded email address. So it only works for blogs where there is only one author.

If you search WordPress support for Author Comment Highlighting you will find another option which checks the email address of the commenter against the email address of the author of the post using WordPress template tags.

I used a mix of both codes to do Author Highlighting on BifSniff.com and it seems to work fine. I just changed the scriptyGoddess code to grab the author email using WordPress tags.

So the solution now looks like this:

In comments.php in your template find the comment loop:
<?php foreach ($< span class="hilite">commentsspan> as $comment) { ?>

And under it paste in the following:

$isByAuthor = false;
if($comment->comment_author_email == get_the_author_email()) {
$isByAuthor = true;

I then found the code for the list item that represents a comment:
<li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">

and inserted the php to add an author class if the email address matches:
<li class="<?php echo $oddcomment; ?> <?php if($isByAuthor ) { echo 'authorcomment';} ?>" id="comment-<?php comment_ID() ?>">

Or you could follow the ScriptyGoddess method to insert a div around author comments. In the comments of her post there is also a way to add an author image.

I used similar code as above to simply add the text ‘(author)’ after an authors name in a comment, by changing this:
<cite><?php comment_author_link() ?> </cite> Says:

to this:
<cite><?php comment_author_link() ?> <?php if($isByAuthor ) { echo '(author)';} ?> </cite> Says:

Seems to be working pretty well for us so far!

Be Sociable, Share!

10 Responses to “Highlight Author Comments in WordPress”

  1. Natalie Says:

    Thanks for taking the time to share this version of author highlighting. It’s definitely cleaner code for multiple author blogs.

  2. Arun Says:

    Thanks for the tip. I tried it out, and I think `if($comment->comment_author_email == get_the_author_email()) {` should be `if($comment->comment_author_email == get_comment_author_email()) {`

  3. Arun Says:

    Or not. Sorry.

  4. Sarah Lewis Says:

    Thanks for providing this! It came in handy on a site where I control the theme but not the plugins.

  5. Jared Walker Says:

    Thanks for leading me in the right direction. Mine’s up and working.

  6. zeaks Says:

    Could this be updated for wordpress 3.0?

  7. Frank Prendergast Says:

    Hi zeaks, this post was written quite some time ago, these days this is much easier to achieve as WordPress now includes classes on author posts.

    In your css you can style an author comment by using the class .bypostauthor

    Hope this helps!


  1. Unique Author Comment Styles in Wordpress | Build Internet!
  2. Unique Author Comment Styles in Wordpress
  3. 10 Handy WordPress Comments Hacks - FPT Arener – Yes! I Can!

Leave a Reply

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image