0
Q:

unique emails contact form 7

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);
0

New to Communities?

Join the community