Top Commentators Without a Plugin


January 19th, 2010 3 comments

Getting your readers involved in the discussion by leaving comments has always been a hot subject. There are several tips you can try to increase the rate of visitors commenting before leaving your blog. One of those tips is rewarding commentators with something useful – getting their name on the top list.

This is a simple method where you put a small list of top commentators either in your sidebar or any other place you feel is the most appropriate. On the list will be their name, website address and also if you want – their gravatar. Wouldn’t you want to be on the top commentators list on a successful blog? Who wouldn’t right?!

With WordPress all you have to do is use the Top Commentators Widget for it. This widget gives you a good set of options for setting it up just the way you like it. You can exclude users who appear on the list, either dofollow or nofollow the links. Even the possibility to reset the list in every X days to give everybody a fair chance.

This is all nice, but I try to keep the number of plugins and widgets to a minimum and use custom code as much as possible. I know some might disagree with me on this subject but thats my point of view. So after giving the comments database table a quick overview I opened up my code editor and started writing down some code.

Quit the chitchat, give me the code already

Ok, but first lets clear what this piece of code does. It creates a top commentators list with their name, website address, number of comments and a small gravatar. And most important part – exclude the site authors.

For this I created a small function that does all that (I prefer using functions, this keeps the overall code much cleaner). Just copy/paste this function inside your functions.php file:

function topCommentators( $limit = 5, $show_avatar = true, $avatar_dimensions = 40 ) {
 
    global $wpdb;
 
    $result = $wpdb->get_results('
        SELECT
            COUNT(*) AS comment_count,
            comment_author_email,
            comment_author,
            comment_author_url
        FROM
            '.$wpdb->comments.'
        WHERE
            comment_type!="pingback" AND
            comment_approved="1" AND
            user_id="0"
        GROUP BY
            comment_author_email
        ORDER BY
            comment_count DESC,
            comment_author ASC
        LIMIT '.$limit
    );
 
    foreach( $result as $commentator ) {
 
        ?>
        <li>
            <?php 
 
            if( $show_avatar ) {
 
                ?>
                <span class="left avatar">
                    <?
 
                    if( $commentator->comment_author_url ) {
                        ?><a href="<?php echo $commentator->comment_author_url;?>" title="<?php echo $commentator->comment_author;?>" target="_blank"><?php
                    }
 
                    echo get_avatar( $commentator->comment_author_email, $size = $avatar_dimensions );
 
                    if( $commentator->comment_author_url ) {
                        ?></a><?php
                    }
 
                    ?>
                </span>
                <?php 
 
            } 
 
            if( $commentator->comment_author_url ) {
                ?><a href="<?php echo $commentator->comment_author_url;?>" title="<?php echo $commentator->comment_author?>" target="_blank"><?php
            }
 
            echo $commentator->comment_author;
 
            if( $commentator->comment_author_url ) {
                ?></a><?php
            }
 
            ?>
            <em>Comments: <?php echo $commentator->comment_count?></em>
        </li>
        <?php
 
    }
 
}

Now to make the list visible just copy/paste this to where the list should appear:

topCommentators();

This uses the default settings: limit 5, show gravatars, gravatar size 40px. Here’s a list of other variations:

#-- Limit 10 commentators, show gravatars, gravatar size 48px
topCommentators(10, true, 48);
 
#-- Limit 5 commentators, don't show gravatars
topCommentators(5, false);

Give it a nice design with CSS and you have a very valuable add-on for increasing comments on your blog.

Now what?

After reading this post maybe you'd be interested in reading some related posts?
You can also help me spread the word or leave a comment.
If you're planning on leaving maybe you'd like to subscribe via RSS Feed
or follow me on Twitter.

Spread the word

3 Comments

Leave a reply