0

Good day all, I want to limit registered users to 1 comment per post in WordPress, i used this code below in my theme's function.php file and have also checked questions that may seem to have my answer on the platform all to no avail.

In this case of this it displays the message "You have commented" on the top bar of my site and does nothing.Hope to find help.

global $current_user, $post;
if ( !is_user_logged_in() ) {
    comment_form();
} else {
    $usercomment = get_comments(array('user_id' => $current_user->ID, 'post_id'=>$post->ID) );
    if($usercomment) { 
        echo '<p>You have commented</p>';
    } else {
        comment_form(); 
    }
}
  • Where do you use this code? – Max Yudin May 31 '17 at 7:50
  • You should put this code to the template file where the comment_form() resides. – Max Yudin May 31 '17 at 7:59
  • 2
    Possible duplicate of How to limit users to one comment per post – Max Yudin May 31 '17 at 8:00
  • 1
    Theme template file! Nobody in the right mind edit files from wp-includes. Also, turn the debugging on during development. – Max Yudin May 31 '17 at 8:39

Your Answer

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

Browse other questions tagged or ask your own question.