1

I use this code to limit the comments to 1 for registers users and works fine. The problem is that when i click to "go back" button in the browser i can comment again. Is there any effective way to limit the comments?

Thank you!

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();
}

1 Answer 1

1

I think that hook pre_comment_approved should be perfect in this case.

Something like this should do the job:

function my_pre_comment_approved($approved, $commentdata) {
    // you can return 0, 1 or 'spam'
    if ( $commentdata['user_ID'] ) {
        $args = array(
            'user_id' => $commentdata['user_ID'],
            'post_id' => $commentdata['comment_post_ID']
        );
        $usercomment = get_comments( $args );
        if ( 1 <= count( $usercomment ) )
            return 0;
    }
    return $approved;
}
add_filter('pre_comment_approved', 'my_pre_comment_approved', 99, 2);

Your filter should return:

  • 0 (i.e. 'false') if the comment should be disapproved
  • 1 (i.e. 'true') if the comment should be approved
  • 'spam' if the comment should be marked as spam
8
  • thank you for you answer but this didn't work for me. I also don't want comments go for approvement i need to restrict user for making second comment in the same post. Commented Aug 9, 2013 at 21:51
  • Have you tried it? What exactly "didn't work"? Commented Aug 10, 2013 at 8:33
  • I put it incide functions.php i left a comment and then i clicked the "go back" button in firefox and then i could comment again... Commented Aug 10, 2013 at 10:31
  • 2
    as 'urder_ID' do you mean 'user_ID' ? Commented Aug 13, 2013 at 8:07
  • Yes, of course it should be user_ID (updated unswer) Commented Aug 13, 2013 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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