[wp-hackers] function to show total comments by user

danilo danixland at gmail.com
Sun Jan 16 18:41:59 UTC 2011


Hello everybody,
I've written a function that shows the total number of comments from a 
given user, being it registered or not on the site:

function user_comments_count($comment_author) {
   $author_comm = $comment_author;
   global $wpdb;
   $comment_counter = $wpdb->get_results( $wpdb->prepare(
     "SELECT comment_author, COUNT( * ) AS total FROM {$wpdb->comments} 
WHERE comment_approved = 1 AND comment_author = %s", $author_comm), 
ARRAY_A);
   foreach ($comment_counter as $count) {
     echo $count['total'];
   }
}

I use it inside the comment loop in the comments.php template as:

<?php user_comments_count( get_comment_author() ); ?>

And it returns the total number of comments.


Now my questions are, have I reinvented the wheel? While I wasn't able 
to find a function within WordPress that does exactly this, maybe you 
can point me to something I've missed in my research...

Is this secure enough?? I've almost never worked directly with the 
database, so I followed the codex which stated that using 
$wpdb->prepare() is a safe way to sanitize db queries, but maybe there 
are safer way to do this same thing, what you think?

Do you think it would be useful to include this in future releases of 
WordPress? I think it would be a nice addiction, what about you?


Thanks in advance.

--
Danilo aka danix
http://danixland.net


More information about the wp-hackers mailing list