Contact form 7 prevent duplicate or multiple submission
Hello buddy, in this article, we will learn about how to prevent multiple submissions or duplicate submissions in contact form 7 with example.
we will also show a unique validation message at the bottom of the form field, to implement this we will need only one plugin that names contact form 7 DB addon. we will follow the below steps to implement multiple submissions in contact form 7.
Step:1 Install cfdb plugin
Cfdb plugin is not officially on the WordPress plugin store, you have to download it from Github and then import the plugin manually.
Click this link to download cfdb plugin from GitHub.
I have added the image for your reference to upload the zip file.
Step:2 Add Code to the function.php
Now go to your theme editor file and open function.php and add the below code to validate your email. The below code ensures one email entry each form.
function is_already_submitted($formName, $fieldName, $fieldValue) {
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$exp = new CFDBFormIterator();
$atts = array();
$atts['show'] = $fieldName;
$atts['filter'] = "$fieldName=$fieldValue";
$atts['unbuffered'] = 'true';
$exp->export($formName, $atts);
$found = false;
while ($row = $exp->nextRow()) {
$found = true;
}
return $found;
}
/**
* @param $result WPCF7_Validation
* @param $tag array
* @return WPCF7_Validation
*/
function my_validate_email($result, $tag) {
$formName = 'Marathon Registration'; // Change to name of the form containing this field
$fieldName = 'email_123'; // Change to your form's unique field name
$errorMessage = 'Email has already been submitted'; // Change to your error message
$name = $tag['name'];
if ($name == $fieldName) {
if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
$result->invalidate($tag, $errorMessage);
}
}
return $result;
}
// use the next line if your field is a **required email** field on your form
add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);
Here I have used my formName Marathon Registration you can use yours second you have to change your email field name to email_123. means [email* email] to [email* email_123] in your contact form 7.
Code to validate mobile number
The Below code will ensure one mobile number entry for each form registration.
function is_already_submitted($formName, $fieldName, $fieldValue) {
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$exp = new CFDBFormIterator();
$atts = array();
$atts['show'] = $fieldName;
$atts['filter'] = "$fieldName=$fieldValue";
$atts['unbuffered'] = 'true';
$exp->export($formName, $atts);
$found = false;
while ($row = $exp->nextRow()) {
$found = true;
}
return $found;
}
function my_validate_mobile($result, $tag) {
$formName = 'Marathon Registration'; // Change to name of the form containing this field
$fieldName = 'your-mob-no'; // Change to your form's unique field name
$errorMessage = 'Mobile number has already been submitted'; // Change to your error message
$name = $tag['name'];
if ($name == $fieldName) {
if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
$result->invalidate($tag, $errorMessage);
}
}
return $result;
}
add_filter('wpcf7_validate_number*', 'my_validate_mobile', 10, 2);
Test the integration
I have applied the same code to my WordPress website, now it’s time to check the code integration.
Fuction.php
contact form 7 code
Result
I hope you understood how to prevent duplicate form submission in wordpress using contact form 7, still have the confusion fee free to comment.
Thumbs Up!
You saved my life.
Is there a way to get this to work for multiple forms?