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(); 
    }
}
4
  • Where do you use this code?
    – Max Yudin
    Commented May 31, 2017 at 7:50
  • You should put this code to the template file where the comment_form() resides.
    – Max Yudin
    Commented May 31, 2017 at 7:59
  • 2
    Possible duplicate of How to limit users to one comment per post
    – Max Yudin
    Commented May 31, 2017 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
    Commented May 31, 2017 at 8:39

0

Your Answer

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

Browse other questions tagged or ask your own question.