Build Internet has a brand new theme, and that's only the beginning. Read the full story or hide this bar

Unique Author Comment Styles in WordPress

Unique Author Comment Styles in WordPress


When I first looked into making styled author comments, I got a bit of a headache sifting through all of articles out there. There weren’t many that solved the problem from start to finish. The most helpful introduction I found was on the Aonach Consulting blog.

The above post will be the starting point for today’s tutorial. We’ll use some PHP and WordPress magic to sort through and style a variety of comments based on the role of the author.

The Goal

By the end of this tutorial, we’ll have set up a comment system which automatically highlights comments according to the commenter’s role. For our example, we’re going to aim for an end result similar to the screenshot below. Comments will be highlighted based on roles of guest, author, and blog administrator.

The live demo below is meant to illustrate the end HTML and CSS result, and is not running on WordPress.

Comment Variety Styles

Sort Through Each Comment

In your WordPress theme folder, open up the file “comments.php” for editing. This is the WordPress file responsible for compiling and displaying all of the comments for a given post. All of the code in this section will be inserted into this file.

To get situated, locate the following line of your comments.php file:

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

This is the start of the commenting loop. We’ll be writing all of our (non-CSS) code inside of it, because each comment will need to be individually checked. The PHP loop below will do just that. It will check each comment, and determine whether or not it was made by the author based on the email used to post/comment.

This loop will also determine which comments are made by blog owners specified by email. In this case, there are two blog administrator emails (guest2@testdemo.com and guest@testdemo.com).

Just below the start of the commenting loop, copy in this block of PHP:

<!--Check if comment is by the author-->
        <?php

        $isByAuthor = false;
        $isByAdmin = false;

        if($comment->comment_author_email == get_the_author_email()) {
            $isByAuthor = true;
        }elseif($comment->comment_author_email == "guest2@testdemo.com" || $comment->comment_author_email == "guest@testdemo.com"){
            $isByAdmin = true;
        };
        ?>

After we’ve determined the nature of the comment, we’ll create the comment from the results. You should already have the list item tag for each comment structure in place, so you’ll want to edit it to match the below example. The most notable change is the PHP conditional statement which assigns a different class to each list item depending on whether or not it is made by an author or administrator.

<li class="<?php if($isByAuthor) { echo 'author-comment';}elseif($isByAdmin){ echo 'admin-comment'; }?>" id="comment-<?php comment_ID() ?>">
            <div class="comment-padding">

                <div class="gravatar-col">
                    <?php echo get_avatar( $comment, 64 ); ?>
                </div>

                <div class="comment-col">

                <cite><?php comment_author_link() ?></cite>

                <?php if ($comment->comment_approved == '0') : ?>
                    <p><em>Your comment is awaiting moderation. Sit tight!</em></p>
                <?php endif; ?>

                <p class="datestamp"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a></p>
                <?php comment_text() ?>
                </div><!--End of comment column-->
            </div>
        </li>

At this point, you’ll end up with all of your comments shown as usual but with class names (e.g. authorcomment) that represent their type.

Base Comment Styles

Before we change the styles for special comments, let’s take a look at the CSS for a normal comment. Essentially what we’ll be doing is setting default styles for each comment, and then using specific class names to overwrite the relevant ones as needed.

/*Comment Styles*/

    ol#comment-zone{padding:12px;}

        /*Default comment (Guest)*/
        #comment-zone li{list-style:none; margin:12px; padding:15px; clear:both; height:74px; background:#F7F7F7; border: 1px solid #DFDFDF; font-family:Georgia, "Times New Roman", Times, serif; font-size:12px;}
            #comment-zone li a{color:#0B5780;}
            #comment-zone li cite {font-style:normal; font-size:14px; font-weight:bold;}
                #comment-zone li cite a{text-decoration:none;}
            #comment-zone li img {border:5px solid #CFCFCF; width:64px; height:64px;}
            #comment-zone li .gravatar-col{clear:left;float:left; min-height:100px; width:90px;}
            #comment-zone li .comment-col{float:left;padding-right:30px; min-height:100px; width:420px;}

Style the Author Comment

This is your kingdom; speak with authority. Use a color that will stand out and grab attention.
Highlighted Author Comment
The CSS below will overwrite the base styles to achieve the unique look above.

/*Author Comments Styles*/
        #comment-zone li.author-comment{background:#1F1F1F; color:#F7F7F7; border:1px solid #474747;}
            #comment-zone li.author-comment img {border:5px solid #474747;}
            #comment-zone li.author-comment a{color:#b6d7e9;}

Handling Multiple Owners

We could apply this idea to blogs run by multiple authors. Take Build Internet! for example. Let’s say Sam wanted to make a comment on this post. Even though he was not the author, I would still like for him to stick out from the rest even though he did not write it.

Ideally, the author would have the largest visual hierarchy, then blog admins, followed by guest comments. In this example, there is only a slight difference of shading involved between administrator and author comments. The CSS is essentially the same between author and administrator comments:

/*Admin Comment Styles*/
        #comment-zone li.admin-comment{background:#333; color:#F7F7F7; border:1px solid #474747;}
            #comment-zone li.admin-comment img {border:5px solid #474747;}
            #comment-zone li.admin-comment a{color:#b6d7e9;}

Alternatively you could take the route of assigning each authors their own unique highlight color. You would simply just have to assign more possibilities to the initial identifying loop.

Stand Out More

With the concepts of this tutorial there is no limit to the amount of customization possible in comment styles. A dedicated designer might even style based on things like nationality (via IP address) or any number of other identifiers. Think about it, and if you come up with any fresh ideas please share in the comments below.

To make life easier, I’ve included both a sample comments.php file and the html/css version in the source files. The two of them should help to get your own comment highlighting rolling, especially if you’ve gotten a little lost from different theme structures. If you have any leftover issues, let us know and we’ll see if we can set you straight!

Wordpress.com stats not installed! Posted Sunday, April 26th, 2009 / Back to Top

I this post. Tweet
SPONSOR

34 Comments 7 Mentions

  1. Igor Mattos Author Editor

    Hey!
    Really nice post, i was looking for that for some time already.

    April 26, 2009 · Reply

  2. izzat aziz Author Editor

    this is really good tutorial. really useful for multi-author blog. thanks.

    izzat aziz’s last blog post..20 Comment Design For Inspiration

    April 27, 2009 · Reply

  3. Marvin Author Editor

    really helpful, thanks for that!

    Marvin’s last blog post..Ein Herz für Blogs

    April 27, 2009 · Reply

  4. Brandon Cox Author Editor

    Thanks! I think this is one of the often neglected areas of design – quite helpful of you to provide this tutorial!

    April 27, 2009 · Reply

  5. Andrew Houle Author Editor

    Nice tutorial Zach! I’ll be implementing this on MyInkBlog soon :)

    Andrew Houle’s last blog post..10 Awesome Free Serif Fonts

    April 27, 2009 · Reply

  6. Kawsar Ali Author Editor

    nice tutorial. I have this method in my site. Slightly different way. Thanks for sharing

    Kawsar Ali’s last blog post..40 Logo Design Tutorials

    April 27, 2009 · Reply

  7. Ronny Bendiksen Author Editor

    How can you do the same thing with threaded comments in WP 2.7?

    April 28, 2009 · Reply

  8. Roland Kopp-Wichmann Author Editor

    Great post! I was searching for something like that for a while. Wonder how it will work out on my blog. Thanx

    Roland Kopp-Wichmann’s last blog post..Neu: E-Werkstatt für Selbständige, Existenzgründer und Freiberufler

    April 28, 2009 · Reply

  9. emoruffino Author Editor

    this will work really well when i update the design on my blog… TY!

    April 29, 2009 · Reply

  10. Jarryd Author Editor

    What happens when an author who is an admin comments? Obviously it will pick up that they are an author first, but should there perhaps be a style specifically for author-admins? Just a suggestion and nice work :)

    April 29, 2009 · Reply

    • Zach Dunn Author Editor

      @Jarryd

      It would not end up being a problem, because the admin state is only checked if the commenter is not the author. You would need to have to separate If loops rather than an if/elseif in order to pick out author/admin.

      April 29, 2009 · Reply

  11. mssmotorrd Author Editor

    It’s the first time I commented here and I must say you share us genuine, and quality information for bloggers! Good job.
    p.s. You have a very good template for your blog. Where did you find it?

    May 3, 2009 · Reply

    • veska Author Editor

      Nice template, indeed.

      November 24, 2010 · Reply

  12. web design company manager Author Editor

    Nice tutorial! Thank you. Some moments are still not clear to me but I will try to investigate them by myself.

    May 12, 2009 · Reply

  13. Dhini Author Editor

    In my comment.php without only
    So How?

    Dhini’s last blog post..Zomaar cadeau (myn lieve man)

    May 23, 2009 · Reply

  14. Richard Ikeda Author Editor

    Muito Bom =D

    July 6, 2009 · Reply

  15. Ben Author Editor

    Thanks took me ages to find this!
    Now implemented in my comments
    Thanks
    .-= Ben´s last blog ..Update your EEE PC Bios from a USB Drive – A Guide =-.

    July 10, 2009 · Reply

  16. mpiftex Author Editor

    This is a good trick but the problem is that if a malicious commenter enters the admin’s e-mail address, it will appear like it’s him, stealing not just the bg color but the gravatar too (i believe)

    The latest version of wordpress gives classes like “comment-author-[name of author]” to the comments if the commenter is signed in. So you can play around with these classes on the css.

    July 18, 2009 · Reply

  17. mikle Author Editor

    Nice tutorial! Thank you.
    .-= mikle´s last blog ..Главная страница =-.

    August 1, 2009 · Reply

  18. jason Author Editor

    It all depends on your database structure. I myself, preffer using sort of a class checking script which I wrote some time ago. It requires some extra fields in database but works as good as this one does

    August 1, 2009 · Reply

  19. a_usman Author Editor

    wow, that is really great tutorial…. I will try to implement in my theme. I have just enabled it to be compatible with threaded comments.
    .-= a_usman´s last blog ..One Email for Multiple Twitter Accounts =-.

    August 21, 2009 · Reply

  20. Ennis Author Editor

    Any way to make this into a plugin? not sure why, but i’ve modified my code to match above, included the css, but the changes dont take effect in my comments. Not sure why

    December 18, 2009 · Reply

  21. Steven Author Editor

    @ a_usman
    Can you please update us with the code fro new threaded template.

    Thanx

    February 17, 2010 · Reply

  22. Leslie Ardinger Author Editor

    Fabulous tut on this whole comment.php file!! I am also impressed by your incredible knowledge at your young ages! Kids nowadays!! I am having a problem though with your downloaded comments.php file. This is the line (mine is line 39)

    <li class="” id=”comment-”>

    I am receiving a parse error: Parse error: syntax error, unexpected ‘?’ (my… ” id=”comment-…is a black color in my editor which indicates that something is wrong. Any thoughts?

    March 11, 2010 · Reply

    • Zach Dunn Author Editor

      @Leslie

      Thanks for the compliments! We’d like to think that we do our part to contribute on behalf of our generation.

      Now let’s talk about your issue. I’m actually going to refer you to an updated version of the comment highlighting system found here. This was originally posted in April 2009, and was revisited again in September that year. You can find the updated post (and hopefully your solution) in the “Automatically Highlight Admin Comments in WordPress” tutorial.

      March 11, 2010 · Reply

  23. Leslie Ardinger Author Editor

    Sorry guys! I figured it out…I was missing a quote after my admin email! Time for a break! Thanks again for your time and effort putting this together!

    March 11, 2010 · Reply

  24. Web Design Author Editor

    awesome stuff get post
    thanks

    April 22, 2010 · Reply

  25. hussain Author Editor

    Such a Great post! I was searching for something like that for a while. I am really wonder how it will work out on my blog. Thanx

    July 27, 2010 · Reply

  26. Barnevern Author Editor

    Very useful. This is the second “google finding” on this site in an hour (same topic)!

    Thanks again for sharing

    August 16, 2010 · Reply

  27. Samatha Ring Author Editor

    I’ve seen this comment style, but I think it’s much better if one of the color is much lighter than the other one. The more lighter it is the more easier someone can recognized different comments.

    October 23, 2010 · Reply

  28. Valentin Author Editor

    Thanks, i was looking for something like that very long. :-)

    October 30, 2010 · Reply

  29. Diesel Engine Author Editor

    Everything on a website that is unique is a lot better than something copied from anywhere. And for sure it will make your website to have better ranking at visitors.

    November 6, 2010 · Reply

  30. Anne Author Editor

    I thought of something, can you make a tutorial where we could actually change the color used in the commenting background? I would like to make use of a more lighter color than two dark ones like the above sample. I am really clueless with the coding.

    December 10, 2010 · Reply

  31. Groho Author Editor

    I was looking for this, but I don’t find any “foreach”

    May 1, 2011 · Reply

 

Join the Conversation

Back to Top / Comment RSS

2011 Build Internet. Created by One Mighty Roar. Icons by Komodo Media. Back to Top