WordPress Tips & Tricks: Author Comment Highlighting
When creating WordPress themes, I love it when it comes to styling the comments. I always find it a challenge to do something different. But my favourite part of comment styling is the author highlighting. It’s probably one of the most obvious things to add to a theme when developing one (or using someone elses) and it’s fairly straight forward too, but for those that are new to WordPress and themes, may not know how to build it into the theme without resorting to (yet another) plugin.
First of all, let’s take a look at different styles of author highlighting. The positibilities are almost endless. You can just use a different colour for your own comment or a completely different style and font, font-size or colour. It’s really up to you and how your theme looks. Check out some examples below:
The first example is from my own blog (the one you’re reading). I’m using 3 separate images in my comment styling. Two for alternating (the greys) and one for author highlighting (the blue). Example two is from the Simple In Pink theme. You can clearly see the difference. Author comments have a yellow background rather than white.
The third example is taken from Unintentionally Blank. I like what Phil has done with his comments, using gradients. When it comes to his own comments, the gradient itself is in reverse, and there is no flower in the top right-hand corner. The last example is from Zeta Brigden’s theme (not yet online) and the author highlighting is much more simple compared to the rest, using blue text instead of green.
So really, the possibilities of author highlighting are endless. Now onto implementing it for your next WordPress theme or for yourself.
1. The first thing you will want to do is open comments.php, which should come with any good WordPress theme. Dive into the template and find the following line (or anything similar):
<li class=”<?php echo $oddcomment; ?>” id=”comment-<?php comment_ID() ?>”>
This is the line we will be adjusting to add author highlighting. Change this line to:
<li class=”<?php if ($comment->comment_author_email == “email@yoursite.com”) echo ‘author’; else echo $oddcomment; ?>” id=”comment-<?php comment_ID() ?>”>
That’s it. Obviously, you will want to change the email@yoursite.com to your own email address so it highlights your comment (or leave it blank if putting it into a theme for others). echo ‘author’ can remain or it can be changed to reflect your name. For example, for my theme, I’ve used echo ‘nyssa’. This is what controls the style of the author highlighting, which then leads us onto point 2.
2. Open style.css. You’ll now want to add the style for your comment. You could use something like the following:
.author {
background:#FFFFFF url(images/someimage.gif) top repeat-x;
color:#000000;
}
One problem you may run into is that the rest of the comment styling is overiding your author styling. This is easily fixed by using !important to override the general comment styling:
.author {
background:#FFFFFF url(images/someimage.gif) top repeat-x !important;
color:#000000 !important;
}
Now you should have author highlighing. The one downfall to using this method, is that you’ll need to dive into comments.php if you ever need to change your address, or your theme users will need to, and not everyone is in-the-know enough not to get lost or just don’t know what they’re doing. One way around this is by using theme options within the theme, but that’s a whole other story (coming soon…).
3. If for some reason you want to highlight two or three authors (good if you’re a part of a multiple authored blog environment), it’s as simple as “adding on”:
<li class=”<?php if ($comment->comment_author_email == “author1@yoursite.com”) echo ‘author1′; else if ($comment->comment_author_email == “author2@yoursite.com”) echo ‘author2′; else if ($comment->comment_author_email == “author3@yoursite.com”) echo ‘author3′; else echo $oddcomment; ?>” id=”comment-<?php comment_ID() ?>”>
Each author can have his or her own styling. Just follow point 2 and add more classes to your stylesheet (style.css).




hey a good tutorial I have bookmarked :) it
Nice one Nyssa, and thanks for using me as an example!
I never thought about making my comments highlighted when designing akamike, maybe I should do a little tweaking this weekend.
Thanks for the tips :)
Arpit: Thanks :)
Phil: No problem. I love your gradients ;)
Mike: I’ll stop by and see if you do anything :P
[...] WordPress Tips & Tricks: Author Comment Highlighting - nyssajbrown.net [...]
[...] my post, WordPress Tips & Tricks: Author Comment Highlighting, I mentioned the downfalls to using that particular method: having to manually change the email [...]
[...] comment highlighting. Author Comment Highlighting and Author Highlighting Through Theme Options provide information about styling author’s [...]
[...] comment highlighting. Author Comment Highlighting and Author Highlighting Through Theme Options provide information about styling author’s [...]
The only problem I see with how you’re doing this is that anyone who enters the blog author’s email address would appear as the author himself. If there were some way to use an if statement with the user id of the commenter posting, that would be much more desirable. Unfortunately, this sounds a little easier than it actually is.
@Jared: Not sure that is really a problem as it wouldn’t happen often. And if it did, you’d have to be asking why someone would be using the blog authors email address and not their own when leaving a comment.
If you’re talking about a blog with multiple authors, I could see it possibly happening more often, but I’d still think that they should probably be using their own email address in their profile so if they’re logged in leaving a comment, it uses their own email address.
[...] Also, my own comments are highlighted with a darker colour, with thanks to Nyssa’s post on Author Comment Highlighting (a recommended read for those developing wordpress themes. I like what I have done with them, [...]
In the snippet you need to copy and paste into comments.php it says you need to replace email@yoursite.com with your own e-mail address, but I get an error because you use an @ in an e-mail address and I guess you can’t ‘cos I get a syntax errors. Do you know of any ways to fix this? Thanks in advance. :)
@Nanda: Sorry, no I don’t. It works fine for myself and with every other theme I’ve used it for, so it can’t be the @.
The only thing I could possibly think of is that it’s the quotation marks (copy/paste sometimes doesn’t read it correctly) or something has messed up the code slightly.
Have a look at this image to see the code in it’s php form with syntax highlighting to see it clearly.
If you still can’t figure it out, feel free to send me an email via my contact form and I’ll have a look at your code to help you out. :)
[...] comment highlighting. Author Comment Highlighting and Author Highlighting Through Theme Options provide information about styling author’s [...]
[...] comment highlighting. Author Comment Highlighting and Author Highlighting Through Theme Options provide information about styling author’s [...]
[...] comment highlighting. Author Comment Highlighting and Author Highlighting Through Theme Options provide information about styling author’s [...]
[...] comment highlighting. Author Comment Highlighting and Author Highlighting Through Theme Options provide information about styling author’s [...]
I like the last one. Probably, I will use it for my blog if it is ok to you.
[...] Author Comment Highlighting and Author Highlighting Through Theme Options gives detailed information on styling author’s comments. [...]
This is so cool. I was wondering how that was done. Now I can do it to my blog. :) BTW: I’m totally jealous of your design. ;D
Glad it helped, Jenny. :)
And I guess that means you like the design then. ;) Glad you do, though it’s going bye-byes soon!
I’m not a fan of pink. But your blog looks stylish. Congratulations!
[...] Author Comment Highlighting and Author Highlighting Through Theme Options provide information about styling author’s comments [...]
[...] comment highlighting. Author Comment Highlighting and Author Highlighting Through Theme Options provide information about styling author’s [...]
If I may advertise a little, then I have made a plugin called “Comment Highlighter”, which can handle such modifications without starting to “hack” you comments.php file.
You can set up rules based on emails, ID’s, names, etc. and then assign a class name to that rule. All that’s left after that is the style.css file, where you define the style as you also mention.
If interested then the plugin can be found here: http://wordpress.org/extend/plugins/comment-highlighter/
Hi Jan. I’m more than aware there are plugins for this type of thing. This is for people that would rather not use a plugin (like myself). :)