How To Display Author’s Comment Count in WordPress Without a Plugin

In this tutorial, I will show you how to display the number of comments an author has posted in the post´s comment section without the need of a plugin, as shown in the image below:

Counting Author's Comments.

Everytime someone leaves a comment on any post, the Author´s comment total count will show up next to his/her name.

Here are the features of the code:

  1. Only approved comments are counted. Pending (and trashed) comments are NOT counted.
  2. Code is tested to work properly on WordPress Version 3.4.1, but it will work on earlier versions.
  3. The code count Authors´ Comments based on their email. Even unregistered users comments are counted.
  4. There is still an issue with the code: The code still add a “number” for pingbacks/trackbacks. I said a “number”, because this number is some constant and is not an accurate count. Pingbacks/Trackbacks should NOT be counted. There is No email address associated with them. More on that later…

Here is How to display the number of comments by author.

STEP1: Counting Author Comments | functions.php

Open functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-B. Save the file and upload it to the server.

CODE-B: Count Number of Comments per Author

<?php
/************************************CODE-B*********************************
* @Author: Boutros AbiChedid 
* @Date:   July 2, 2012
* @Websites: bacsoftwareconsulting.com/blog/ ; blueoliveonline.com/
* @Description: Add the Comment count per Author based on his/her Email.
* @Tested on: WordPress version 3.4.1 
* If you find this code helpful, please donate to keep new WP codes coming.
* @DO NOT REMOVE THIS SECTION (see terms of use).
****************************************************************************/

function  bac_comment_count_per_user() {
    global $wpdb;
    $comment_count = $wpdb->get_var(
    'SELECT COUNT(comment_ID) FROM '. $wpdb->comments. ' 
    WHERE comment_author_email = "' . get_comment_author_email() .'"  
    AND comment_approved = "1"
    AND comment_type NOT IN ("pingback", "trackback")'
    );
      
    //Discriminate between singular and plural.
    if ( $comment_count == 1) {
        echo ' (1 comment)'; 
    }
    else {
        echo ' (' . $comment_count . ' comments)'; 
    } 
}
?>

CODE-B Notes:

WordPress provides a class of functions for all database manipulations. WordPress has the global variable $wpdb, which is an instantiation of the class already set up to interact with the database. Always use the global $wpdb variable. Remember to globalize $wpdb before using it in any custom functions.

The $wpdb object can talk to any number of tables, but only to one WordPress database.

The get_var function returns a single variable from the database. Though only one variable is returned, the entire result of the query is cached for later use. The query returns NULL if no result is found.

Line 17 of the code, means that only approved comments are counted.

Line 18 of the code, means that Trackback/Pingback comments should NOT counted.

Line 18, does not work. I am not sure why! More on that later…

Thanks to Pontus for all his help in trying to get the code to work.

CODE-B References:

STEP2: Display the Comment Count

After you added CODE-B to your theme´s functions.php, now you need to display the comment count.

To do that, there are 3 scenarios. It all depends on the theme you are using:

Open your theme´s comments.php file. If in this file you found…

  1. Case1: The wp_list_comments() function with a callback parameter. Something like:
    <?php wp_list_comments('callback=mytheme_comment'); ?>

    Then in this case you need to edit the mytheme_comment function located in your theme´s functions.php file. I will show you an example on how to do that in the next section.

  2. Case2: The wp_list_comments() function with no callback. Something like:
    <?php wp_list_comments(); ?>

    Then in this case, the default WordPress wp_list_comments function is used. In this case, you need to add a custom callback function as a parameter to the wp_list_comments() function. Then you need to define this callback function in the functions.php file. If you need help, feel free to ask. I will show you how to do that with a donation to this blog. Your donation helps keeping new code snippets coming.

  3. Case3: You find nothing similar to Case1 or Case2, but the old way of listing the comments. This is an indication that your theme is very old. If you are using such a theme, either upgrade it or change it. Otherwise, you need to modify the code in comments.php file. Again if you need help, feel free to ask. I will show you how to do that with a donation to this blog.

Real Example of (Case1:) | Diary Theme

To give you an example. One of the WordPress themes I am using for this blog is the modified version of the Diary theme. Therefore, I will be using the Diary theme as an example.

The Diary theme is using Case1: meaning that when I checked the comments.php file and looked for the wp_list_comments(). I found out that this function uses the ´mytheme_comment´ as a callback parameter. This is great since now all I have to do is modify the mytheme_comment function located in functions.php file.

Next, I opened functions.php and located the mytheme_comment() function and modify it as follows:

MODIFIED: Display Number of Comments per Author

<?php
function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class('clearfix'); ?> id="li-comment-<?php comment_ID() ?>">
	 <?php echo get_avatar($comment,$size='38'); ?>
     <div id="comment-<?php comment_ID(); ?>">
	  <div class="comment-meta commentmetadata">
	    <?php printf(__('<strong>%s</strong>'), get_comment_author_link()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?> <span><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?>
        <?php bac_comment_count_per_user(); //Display the total Comment count per Author ?>
	  </span>
	  </div>
	  
      <div class="text">
		  <?php comment_text() ?>
	  </div>
	  
	  <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('Your comment is awaiting moderation.') ?></em>
         <br />
      <?php endif; ?>

      <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>
     </div>
<?php }
?>

Notes:

The mytheme_comment function in the code above, is taken AS IS from the Diary Theme, with the exception of Line 9.
This is the only Line of code needed to modify this function in order to display the total number of Comments per Author. How simple is that!

Styling CODE-B | style.css

To style CODE-B with CSS, you don´t have to do anything extra unless you want to. It will take the same styling as your Comment´s Section. In my case, I did not do anything extra.

Disadvantage of CODE-B

As I mentioned before, CODE-B does NOT ignore trackbacks and pingbacks. The code still add a number for trackbacks and pingbacks. Line 18 of CODE-B does not seem to work. As shown in the image below:

Counting Author's Comments.  Pingbacks and trackbacks.

Even when I replace Line 18 of CODE-B with either one of the following statements:

AND comment_type IN ("comments","")'

OR:

AND comment_type = ""'

Any of the above lines of code did not seem to do the trick. I could not find the logical reason for that. Anyone of the 3 line of codes (including Line 18 already in CODE-B) should have worked! Probably there is something wrong with the theme I am using. I don´t have a solution for this and I would love your feedback on this on how to exclude Trackbacks/Pingbacks from being counted.

Your Turn to Talk

In this tutorial, I showed you how to display each commenter´s total count without a plugin.

How easy did you find this tutorial to implement? Do you have something to add or anything else to say about displaying the number of comments by author? If so, please share your opinion in the Comment section. Your opinion matters, unless it is a Spam.

If you found this post useful, please consider: linking back to it, subscribing by email to future posts, or subscribing to the RSS feed to have new articles delivered to your feed reader, or feel free to donate. Thanks!

About the Author |
Boutros is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer and Blogger. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

2 Responses to “How To Display Author’s Comment Count in WordPress Without a Plugin”

  1. Jordan Mann says:

    You just literally saved my blog. I’ve been looking for an easy to understand tutorial and I found it here. I’m just currently in the process of finishing the codes and I’m very excited about the results. Thank you so much.

  2. Derek says:

    This is cool
    It worked on my news blog
    But the code is just too long