I am using this within a plugin, but it can be used within your theme’s function file.
// Create field to track users' comments numbers
function display_educadme_user_comments_number($user_id) {
if( is_user_logged_in() ) {
global $wpdb;
$user_comments_number = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . get_comment_author_email() . '"');
?>
<h3><?php _e('Participation in the website'); ?></h3>
<table class="form-table">
<tr>
<th>
<label for="user_comments_number">
<?php _e('Number of comments'); ?>
</label></th>
<td>
<input type="text" name="user_comments_number" value="<?php echo $user_comments_number ?>">
</td>
</tr>
<?php
} else {}
}
// Update users' comments numbers
function save_educadme_user_comments_number($user_id) {
if( is_user_logged_in() ) {
if ( !is_super_admin() )
return FALSE;
update_usermeta( $user_id, 'user_comments_number', $user_comments_number );
} else {}
}
add_action( 'show_user_profile', 'display_educadme_user_comments_number' );
add_action( 'edit_user_profile', 'display_educadme_user_comments_number' );
add_action( 'profile_update', 'save_educadme_user_comments_number' );
add_action( 'comment_post', 'save_educadme_user_comments_number' );