The code hooks into Contact Form 7 email validation and checks the submitted email against entries stored in the CFDB7 database. If the same email was already used in the specified form during the last 15 days, the form submission is blocked and an error message is displayed to the user.
Key Features: • Prevents duplicate form submissions • Works with Contact Form 7 + CFDB7 plugin • Checks submissions based on email address • Restricts re-submission for 15 days • Displays a custom validation message • Useful for job application forms, registrations, and limited-entry forms
This solution helps reduce spam, duplicate applications, and repeated submissions while maintaining clean form data.add_action('wpcf7_validate_email', 'custom_cf7_email_validation', 20, 2);
add_action('wpcf7_validate_email*', 'custom_cf7_email_validation', 20, 2);
function custom_cf7_email_validation($result, $tag)
{
$tag_name = $tag->name;
$submitted_email = isset($_POST[$tag_name]) ? trim(sanitize_email($_POST[$tag_name])) : '';
if (empty($submitted_email)) {
return $result;
}
if (email_exists_in_cfdb7($submitted_email, $tag_name)) {
$result->invalidate($tag, 'This email was already used in the last 15 days. Please try again later. [ For inquiries, contact info@gmail.co ]');
}
return $result;
}
function email_exists_in_cfdb7($email, $field_name)
{
global $wpdb;
$cf7_id = 521;
$email = sanitize_email($email);
$data_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT data_id FROM {$wpdb->prefix}cf7_vdata_entry
WHERE cf7_id = %d AND name = %s AND value = %s
ORDER BY id DESC LIMIT 1",
$cf7_id,
$field_name,
$email
)
);
if (!$data_id) {
return false;
}
$submit_time = $wpdb->get_var(
$wpdb->prepare(
"SELECT value FROM {$wpdb->prefix}cf7_vdata_entry
WHERE data_id = %d AND name = 'submit_time' LIMIT 1",
$data_id
)
);
if (!$submit_time) {
return true;
}
$submit_timestamp = strtotime($submit_time);
$now = current_time('timestamp');
return ($now - $submit_timestamp) < 1296000;
}