0

I am trying to prevent guests (not users) from posting comments on one single page if they fill in the same email address. My further goal is to collect their unique addresses for a contest (The best comment of...). I am thinking of writing some code to check if the current poster's email address is already inserted in the comments table before actually inserting the comment. Checking the email existence is pretty clear to me (get_comments()) but I can't think of a proper hook to fire my check. The wp_insert_comment hook would run my code after the comment was inserted. Any idea pls, on this? Thanks!

Later edit: I have found myself a solution with preprocess_comment filter...

1
  • Welcome! Glad to hear you found a solution. Please post your solution as an answer and not an edit to your question so that it can be marked as resolved and future passer-byers can gain benefit from it.
    – YourManDan
    Commented Mar 30, 2023 at 14:14

1 Answer 1

0
function preprocess_mycomment($commentdata) {
    $existing_comments = get_comments( array('post_id' => 31691) ); // I run the code for one specific page only
    foreach ($existing_comments as $comment) {
         $previous_comments = $comment->comment_author_email; // email address send by the current poster
        
         if ( $previous_comments == $commentdata['comment_author_email'] ) { // comparing the current email address with the previous ones in database.
            wp_die('The email ' . $previous_comments . ' has already been used.');
            }
    }

    return $commentdata;

}
add_filter('preprocess_comment', 'preprocess_mycomment');

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.