How to limit users to one comment per post

// Check if user has previously commented the post.
global $current_user, $post;

if ( ! is_user_logged_in() ) {
    // Show the comment form if the user is not logged in.
    comment_form();
} else { // The user is logged in...

    // Get the comments for the logged in user.
    $usercomment = get_comments( array (
            'user_id' => $current_user->ID,
            'post_id' => $post->ID,
    ) );

    // If the user has commented, output a message.
    if ( $usercomment ) { 
        echo '<p>Vous avez déjà signé cette pétition. S\'il vous plaît partager avec votre famille et vos amis</p>';
    } else { // Otherwise, show the comment form.
        comment_form(); 
    }
}

Now the code is checking if the user has previously commented on that specific post instead of the whole site. Hope others will find it helpful.

Leave a Comment