Wordpress Snippet: Display user with the most comments | Tutorial Freak – Online Tutorials

WordPress Snippet: Display user with the most comments

Have you ever wanted your visitors to post their views and engage them? Want to learn a trick which would increase competition among visitors to post more and more? Well, the best and simple thing you can do is – Display a list of top commentors on your blog.

By doing so, other visitors would feel like having their name on the list too and hence they would eventually start contributing to the blog! Isn’t this a cool way to retain your blog visitors?

You may now ask – How to Display users with most comments? Well, with wordpress, everything is pretty easy.

Adding this snippet to the functions.php of your wordpress theme will let you display the top commenter list of users. Use $result->comment_author_email, $result->comments_count, $result->comment_author_url, to add additional parameters to this snippet.

function top_comment_authors($amount = 5) {
global $wpdb;
$results = $wpdb->get_results('
    SELECT
    COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
    FROM '.$wpdb->comments.'
    WHERE comment_author_email != "" AND comment_type = "" AND comment_approved = 1
    GROUP BY comment_author_email
    ORDER BY comments_count DESC, comment_author ASC
    LIMIT '.$amount
);
$output = "<ul>";
foreach($results as $result) {
    $output .= "<li>".$result->comment_author."</li>";
}
$output .= "</ul>";
echo $output;
}

After adding the above code to your functions.php file, just add the below code to wherever you would like the list to appear (usually the sidebar widget).

<? top_comment_authors(); ?>

Great trick right? Please feel free to post your views on this. Commenting does not cost a dime!

Author, Blogger, SEO Expert, Working Professional and a Student! I play so many roles in life and love what i do. I enjoy each moment of my life. Do follow us on Facebook

Sagar – who has written posts on Tutorial Freak – Online Tutorials.


Help keep us alive & kicking, we need coffee 24/7, please donate, thanks!
Embed

Leave a Reply