Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I found this code here on WPSE:

global $current_user, $post;
$args = array( 'user_id' => $current_user->ID, 'post_id' => $post->ID );
$usercomment = get_comments( $args );
if ( 1 <= count( $usercomment ) ) {
    echo 'disabled';
} else {
    comment_form();
}

Note: It seems to limit the number of comments per post per user.

I want to limit the number of comments per registered user per day.

I would like to be able to change the amount of comments allowed per day in the code, please.


If someone could help me with the correct code it would be very much appreciated. Also where would I put this code in the comments.php file?

Thank you.

share|improve this question
    
Hi Chad, have you put this code in your comments template? –  kaiser Aug 3 '12 at 11:19
    
No I have not because its not what I was looking for its was on the tright track though. Thanks Kaiser. –  Chad Aug 3 '12 at 19:22
add comment

2 Answers

You could pull all of the comments by current user and loop over them to see how may where today or you can create a custom sql Query to select just the count of comments for the last 24 hours, something like this:

global $wpdb,$current_user;
$sql = $wpdb->prepare("
    SELECT count(*)
    FROM wp_comments 
    WHERE comment_author = '%s'
    AND comment_date >= DATE_SUB(NOW(),INTERVAL 1 DAY);"
    ,$current_user);
$count = $wpdb->get_results($sql);
share|improve this answer
    
Thank you for the help Bainternet. DO I just need to put this into my comments.php file then? Not sure where to put this code. Much appreciated! –  Chad Aug 3 '12 at 19:25
    
+1 thats really helpful. –  amit Aug 3 '12 at 22:35
add comment

Here's the working version, I referred the technique by @Bainternet to get it done. Just replace <?php comment_form(); ?> with this code

<?php 
    global $wpdb,$current_user;
    $limit = 5; //this is limit per day per user
    $comment_count = $wpdb->get_var( $wpdb->prepare("
        SELECT count(*)
        FROM wp_comments 
        WHERE comment_author = '%s'
        AND comment_date >= DATE_SUB(NOW(),INTERVAL 1 DAY);"
        ,$current_user->user_login) );

    if($comment_count < $limit) {
        comment_form();
    }   
    else {
        echo 'exceeded comments limit - '.$limit;
    }
?>

-- Tested on Wordpress 3.4.1 with TwentyTen theme installed on_

Note -

  • Simply Hides the comment form from users with more than 5 comment for that given day Regardless of comment status
  • This will show comment form to unregistered users unless we disable it from - Discussion Settings
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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