Unique Field – Prevent Contact form 7 Duplicate Submissions

function is_already_submitted($formPostId, $fieldName, $fieldValue) {

    global $wpdb;

    /*We make a select in the table where the contacts are recorded, checking if the email informed already exists on today's date */

    $entry = $wpdb->get_results( "SELECT * FROM DB_PREFIX_db7_forms WHERE form_value LIKE '%$fieldValue%' AND form_post_id = '$formPostId'" );
    
    // If the select query in the database is positive (the email exists on the current date), it returns the error in the form, in the email field, not sending

    $found = false;
    if (!empty($entry)) {
        $found = true;
    }
    return $found;
}

function my_validate_email($result, $tag) {
    $formPostId = 'FORM_ID'; // Change to name of the form containing this field
    $fieldName = 'your-email'; // Change to your form's unique field name
    $errorMessage = 'This email address is already registered'; // Change to your error message
    $name = $tag['name'];
    if ($name == $fieldName) {
        if (is_already_submitted($formPostId, $fieldName, $_POST[$name])) {
            $result->invalidate($tag, $errorMessage);
        }
    }
    return $result;
}

add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source