We create beautiful & user-friendly websites and provide quality web development services worldwide.
Contact us to get started on your project.


How to Display Every User’s Comment Count in WordPress without a Plugin

Published by Veron Ang | 28

How to Display Most Commented Posts in WordPress Without a Plugin

Whilst browsing the financial analysis website seekingalpha.com, I noticed that every time someone leaves a comment on any article, his total comment count would show up next to his name. This is pretty neat as it gives a community vibe to the site. In fact most discussion forums already have such a feature built in, displaying each user’s total number of posts.

How to Display User/Visitor Comment Count in WordPress without a Plugin

In WordPress, you can do the exact same thing by adding the function below. It checks the commenter’s email address and displays the total number of comments that he has left on your website from past to present. It will work even if the person is not a registered user of the site.

Note: If you are using the Akismet plugin, you may have noticed the new option “Show the number of comments you’ve approved beside each comment author” available since version 2.5. Turning on this option displays the total comment count of each comment author as well, but only within the WordPress admin panel. In our case, we are going to make the comment count publicly viewable.

Display User/Visitor Comment Count without a Plugin

  1. Open your functions.php template file and add the following function.
    function commentCount() {
    	global $wpdb;
    	$count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . get_comment_author_email() . '"');
    	echo $count . ' comments';
  2. Call the function in your comments template (usually comments.php) where you want it to appear, for eg. next to the nickname.
    <?php commentCount(); ?>

About the Author

Veron Ang is a , developer and WordPress expert at Sparklette Studio. She has been building websites for as long as she can remember, and graduated with a bachelor's degree in computer science from the National University of Singapore.


  • New WordPress tutorial: How to Display User/Visitor Comment Count Next to Nickname without a Plugin http://sgwordpress.com/teaches/how

  • so great ! thanks ! i like this code !

  • Thanks for this code snippet!

    I added a little bit of HTML and CSS to make it a superscript with brackets, so that it’s less obtrusive. But it’s nice to have the hard database code already available. ;-)

    • I saw your code. It looks neat indeed. Thanks for sharing :) It’s interesting to see a different take on the same idea.

      • This is my adapted version of your function:

        function commentCount() {
        global $wpdb;
        $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . get_comment_author_email() . '"');
        echo '' . $count . '';

        In my comments.php, there’s just the call to commentCount() – everything’s styled by the corresponding CSS:

        span.user-comment-count { vertical-align: text-bottom; font-size: 1ex; }
        span.user-comment-count:before { content: "("; }
        span.user-comment-count:after { content: ")"; }

        The vertical-align is a bit of a hack; with “vertical-align: super” the number was just too tiny.

        (Lacking preview, I just can hope that I didn’t mistype something and the code snippets will look ok…)

  • Thanks for the code. It certainly looks unobtrusive on your site. I especially like the little tooltip that pops up as well.

    (Following your comment, I have installed the jQuery Comment Preview plugin. People are always pasting codes in here. Wonder why I hadn’t thought of it sooner!)

  • where do we put the code in comments.php?

    • You would put the code at the place where you want it to appear. If you want it next to the commenter’s name, look for “comment_author” in your theme. Every theme is different so it may or may not be in comments.php. if you are using the default WordPress twentyten theme, it would be in the functions.php.

  • Hi, i use your incredible plugin, but i have one problem. for non-users it show wrong number. for example i expect 4 but it show 275, i know it’s something because 275 show on several user. thanks for your help

  • i use this and i think this work perfect:

    function commentCount() {
    global $wpdb;
    $count = $wpdb->get_var(‘SELECT COUNT(comment_ID) FROM ‘ . $wpdb->comments. ‘ WHERE comment_author_email = “‘ . get_comment_author_email() . ‘”‘);
    echo ” . $count . ”;

  • Could we make it displays in sidebar widget instead?

    • I’m guessing you want the widget to display top commentators? I have a comment further down with links to 2 tutorials that show you how to do that.

  • […] 代码来自:http://sgwordpress.com/teaches/how-to-display-user-comment-count-in-wordpress-without-a-plugin… 注意这个博主的函数有点问题,评论里的那个更好使。 […]

  • I love the twitter link beside the comment author link can anyone offer help how to do it, please and than ya!

  • Very Cool… any idea as how to get the top commenters as a list based on that? can i loop trough the users who commented and check?

  • I would like to build this out to query the database and display users who have commented the most. Shouldn’t be too hard but if any php wizards want to whip this out for me I’d be honored. I’d like to use it at http://www.futebolbrasileirao.com and http://www.halecollege.com. Thank you!!

    • Perhaps “Top Commentators Widget” would suit your needs?

  • Thanks for this tutorial, it really helped me out alot!

  • This works great, except users who haven’t entered an email get bundled together. Is there a way to count via registered user name?

  • […] geworden bin ich in einem Beitrag bei Sparklette […]

  • How do I show this on Post page? Not on comment page. I have a page template already.

  • where do we put the code in comments.php?

  • […] also nicht auch in WordPress einsetzen? Nach einer kurzen Recherche bin ich auf einen Beitrag von Veron Ang auf sparklette.net gestoßen. Bei caschy bin ich außerdem in den Kommentaren auf die “ist Kommentar […]

  • Good post, thanks. Modified for more general use:

    function commentCount($uid) {
    global $wpdb;
    $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE user_id = "' . $uid . '"');
    return $count;

  • Thanks for the code snippet idea.

Leave a comment