Styling Comments by Author

Posted by Edward Caissie on May 26, 2009 in WordPress |

As I was working through the various files involved in my WordPress Desk Mess Mirrored theme I discovered I did not like the original comment author CSS class assignment code.

The original code was as follows:

<?php foreach ($comments as $comment) : ?>
  <div class="<?php if ($comment->comment_author_email == "xxx@yyy.com") echo 'administrator'; else echo $oddcomment; ?> item" id="comment-<?php comment_ID() ?>">

It did not thrill me with the choice of using the author’s email address. As far as I see it this could possibly lead to misrepresented comments or replies, if the blog owner was not careful with their contact information and their comment moderation settings.

I started with the idea that there must be a way to check the comment author and compare to the registered users list of the blog. This started my searches and brought me to this codex article: http://codex.wordpress.org/User:Jalenack/Comment_Loop_Beauty with the following code reference:

<cite<?php if ($comment->user_id == '1') echo ' class="admin"'; ?>>
  <?php comment_author() ?>

Which I expanded upon to this:

<?php foreach ($comments as $comment) : ?>
  <div class="<?php if ($comment->user_id == '1') echo 'administrator';
    /* elseif ($comment->user_id == '2') echo 'administrator'; */
    /* add additional user_id following above example, echo the 'CSS element' you want to use for styling */
    <?php endif; ?>

This snippet of code writes the class administrator for the user_id #1 (generally the blog owner) and can then be further modified for each user_id as appropriate, changing the class or not as the design requires.

To find an individual user_id, go to your blog’s Dashboard; under Users, click on Authors & Users. Click on the Username (to edit); now in the address bar you will see a URL address starting with: http://your_blog_url/wp-admin/user-edit.php?user_id=x… , where x (a number) is the user_id for that particular Username.



  • Chip Bennett says:

    I use another method, that also does not expose the author’s email address:

    (wrapped in php tags, of course):

    $isByAuthor = false;
    if($comment->user_id == $post->post_author) {
    $isByAuthor = true;

    Then, inside the comment LI:

    if($isByAuthor ) { echo ‘class=”authorcomment”‘;}

    Then, of course, the style sheet will define li.authorcomment.

    I think it might be a bit cleaner, and isn’t dependent upon knowing the author/admin UserID.

    (I’m sure I picked up this method from someone/somewhere (perhaps the Codex; perhaps not). I’d give credit, but I don’t remember anymore…)

    • JellyBeen says:

      That’s a great little snippet to keep in mind Chip, thanks for sharing it here.

      I am using this method here to differentiate between two, or more, registered members of the blog, giving each registered member (and for that matter it can be extended to non-registered members) a unique style for their comments. For example this reply.

  • Chip Bennett says:

    You could also do something like:

    li class=”user-PHPSTUFFHERE”

    where PHPSTUFFHERE =

    php echo $comment->user_id;

    So UserID 1 would have:

    li class=”user-1″

    And UserID 15 would have:

    li class=”user-15″

    You could also wrap an if statement around the user-id call, and differentiate between registered commenters and non-registered commenters; something like (this may not be *exactly* right, since i don’t know what gets returned for an anonymous comment):

    li class=”PHPSTUFF”

    where PHPSTUFF =

    if $comment->user_id {
    $user_status = registered_user;
    echo $user_status. ” user-“.$comment->user_id;
    } else {
    $user_status = non_registered_user;
    echo $user_status;
    } endif;

    (Feel free to correct if my PHP syntax is wrong there.)

    That way, for a non-registered user, you would get:

    li class=”non_registered_user”

    And for a registered user with user_id 5, you would get:

    li class=”registered_user user-5″

    That way, you could style the comments differently for registered and anonymous users, and also for each user ID.

    (Nice post; thanks for the food for thought! I might try something like this on my own blog.)

  • I do enjoy your comments. They make for interesting reading and great for new ideas. I believe your latest comment would lead to a more automated styling approach whereas my original post is very much a manual approach. FYI, user_id=0 identifies non-registered users.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2009-2017 BuyNowShop All rights reserved.
This site is using the Custom Child-Theme, v1.0.3, on top of
the Parent-Theme Desk Mess Mirrored, v2.5, from BuyNowShop.com