0

I am using WPDiscuz, just an upgraded version of the default wordpress comment system, nothing really special.

At the moment, I am using the code above to get the total comments of users based on their email:

add_filter('wpdiscuz_comment_author', 'my_commentCount', 10, 2);
function my_commentCount($author_name, $comment) {
    global $wpdb;
    $no_comment = "No comments";
    $one_comment = "One comment";
    $more_comments = "% comments";
     $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . addslashes($comment->comment_author_email) . '"');
    $comment_text = my_comment_number_text($no_comment, $one_comment, $more_comments, $count);
        return $author_name .' | <span class="wpdiscuz-comment-count">'.$comment_text.'</span>';
}
function my_comment_number_text( $zero = false, $one = false, $more = false, $number = 0 ) {
    if ( $number > 1 ) { $output = str_replace( '%', number_format_i18n( $number ), $more); } elseif ( $number == 0 ) { $output = $zero; } else { $output = $one; } return $output;
}

After surfing on the internet, I found this blog, which gives their guests badges based on their comment counts:

[removed]

Do you know any way to do this for my blog?

They are using the same code above to get comment count btw.

closed as off-topic by Mark Kaplun, Jack Johansson, cjbj, bueltge, Max Yudin Oct 22 '17 at 15:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

0

Please don't use addslashes to escape things for SQL (it's potentially dangerous), use $wpdb->prepare("SQL with %s placeholders", $string). It will put the data into the placeholder's ... place and take care of escaping, giving you SQL that is safe to execute.

Anyway. Your question is how to give them a different background color based on the comment count?

Why not keep it simple and change

return $author_name .' | <span class="wpdiscuz-comment-count">'.$comment_text.'</span>';

to

$bgcolor = '#CCCCCC'; // default
if($count > 800) $bgcolor = '#FF00FF';
if($count > 500) $bgcolor = '#00FFFF';
if($count > 250) $bgcolor = '#FFFF00';
if($count > 100) $bgcolor = '#800000';
return $author_name . ' | <span class="wpdiscuz-comment-count" style="background-color: ' . $bgcolor . ';">' . $comment_text . '</span>';

Or have I completely missed the point and failed to understand what you are trying to do?

To manipulate the left part of a comment, you can go for something like

add_filter('wpdiscuz_after_label', 'my_badgeCount', 10, 2);
function my_badgeCount($html, $comment) {
    global $wpdb;
    $count = $wpdb->get_var( $wpdb->prepare('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments . ' WHERE comment_author_email = %s', $comment->comment_author_email) );
    if($count > 9) return $html . '<img src="/wp-content/uploads/badge-great.png" alt="great user" />';
    if($count > 6) return $html . '<img src="/wp-content/uploads/badge-good.png" alt="good user" />';
    if($count > 3) return $html . '<img src="/wp-content/uploads/badge-ok.png" alt="ok user" />';
    return $html;
}

You'll have to adapt the conditions and images, obviously. Also note the $wpdb->prepare() use. Not only is it safer, I find it much easier to read, too.

  • $firstcomment = $wpdb->get_var( $wpdb->prepare('SELECT comment_date FROM ' . $wpdb->comments . ' WHERE comment_author_email = %s ORDER BY comment_date ASC', $comment->comment_author_email) ); will get the first comment date for that user. You can parse it with php's strtotime. Then just compare it to time(). – janh Oct 15 '17 at 8:57
  • Is using the 2 codes in your main reply above at the same time okay? – William Oct 15 '17 at 15:30
  • Sure. As long as they have different function names, they'll work side by side. – janh Oct 15 '17 at 16:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.