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

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. – Christopher Aug 9 '13 at 21:51
  • Have you tried it? What exactly "didn't work"? – Krzysiek Dróżdż Aug 10 '13 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... – Christopher Aug 10 '13 at 10:31
  • 2
    as 'urder_ID' do you mean 'user_ID' ? – Christopher Aug 13 '13 at 8:07
  • Yes, of course it should be user_ID (updated unswer) – Krzysiek Dróżdż Aug 13 '13 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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