Quick Tip: Highlight Author Comments in WordPress – The Right Way

written by aext on March 2, 2010 in Quick Tips with 55 comments

This is an old topic that has been covered by many other blogs and magazines–to the extreme, but it needs updating. I’m talking about how to highlight comments posted by the author of the article. The tutorials out there only work for one user ID for the whole site. That is a big mistake, as it didn’t allow for scalability — it only works when you are blogging on your own (and with only one account). We have given it an overhaul, and updated the code.

First, we should take a look at the code from the tutorials which highlight the comment based on the user ID:

.authcomment {
    background-color: #B3FFCC !important;
}

<li class=”<?php

                if (1 == $comment->user_id)
                    $oddcomment = “authcomment”;
                echo $oddcomment;?>” id=”comment…">
                
                ...
                
</li>

To make it work on sites with multiple writers, we need to to get the ID of the author, then check if the ID of the person posting this comment is the same. If it is, we need to make it stand out. We still use the CSS class for post author comment.

.authcomment {
    background-color: #B3FFCC !important;
}

Now, we change the original code at the start of the article with this:

<li class="comment-container
    
    <?php
        $authID=get_the_author_meta('ID');
                                    
        if($authID == $comment->user_id)
            $oddcomment = 'authcomment';
        echo $oddcomment;
    ?>"  id="comment-<?php comment_ID(); ?>">
                                    
        ...
        
</li>

And, that’s it! We hope that you find it useful!