Web Design. Web Development. Web Hosting.
0 items

ContactForm7 – Stop duplicate submissions

Need some help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request support

We had a problem recently whereby a client using Contact Form 7 (CF7) was receiving multiple form submissions from customers and was getting frustrated. The problem was we had removed captcha because its bad for usability and had also removed Google ReCaptcha V3 because it had horrible dependencies on JavaScript that was slowing down the site pages and not meeting googles new metrics for speed.

We found some solutions that allowed for the use of CFDB to check if an email was already submitted, this however presented 2 problems:

  1. There were many forms and many used different variables.
  2. The solution would block multiple submissions FOREVER – which is not ideal.

What we wanted was to just block the ability to submit multiple times upon action, not forever, and did not want to have to edit all the various contact forms. So we were stuck between an rock and a hard place because a custom solution was needed.

What we settled on was a simple but effective solution, when a form is submitted store the next time the customer can submit a form in a session variable, if they try and submit anything until the time runs out show a simple error explaining that a form was already submitted.

Stop all form submissions for ~1 minute

Here is our first solution to stop duplicate form submissions in Contact From 7, this code will stop any and all submissions for ~1 minute, add this to your wordpress theme functions.php:

<?php 
/**
 * Hook CF7 - stop duplicate submissions
 * By: Dean Williams
 * URL: https://webdesires.co.uk
 */
 
add_filter( 'wpcf7_validate', 'maybe_skip_mail2' );
function maybe_skip_mail2($result, $tags = '') {
 
 $currentformInstance = WPCF7_ContactForm::get_current();
 $contactformsubmition = WPCF7_Submission::get_instance();
 $mail = $currentformInstance->prop('mail');
 $data = $contactformsubmition->get_posted_data();
 //stop multiple submissions
 if ( ! session_id() ) {
 session_start();
 }
 if (isset($_SESSION['cf7_form_timer'])) {
 if ($_SESSION['cf7_form_timer'] > time()) {
 $data = array_pop($data);
 foreach ($data as $type => $d) {
 $result->invalidate($type, "You have already submitted a contact form, please wait at least 1 minutes before submitting another.");
 }
 } else {
 $_SESSION['cf7_form_timer'] = strtotime('+1 minute', time());
 }
 } else {
 $_SESSION['cf7_form_timer'] = strtotime('+1 minute', time());
 }
 
 return $result;
}
?>

Stop duplicate form submissions for ~1 minute

Here is our second solution to stop duplicate form submissions in Contact From 7, this code will stop any identical form submissions ~1 minute, add this to your wordpress theme functions.php:

<?php 
/**
 * Hook CF7 - stop duplicate identical submissions
 * By: Dean Williams
 * URL: https://webdesires.co.uk
 */
 
add_filter( 'wpcf7_validate', 'maybe_skip_mail2' );
function maybe_skip_mail2($result, $tags = '') {
 
 $currentformInstance = WPCF7_ContactForm::get_current();
 $contactformsubmition = WPCF7_Submission::get_instance();
 $mail = $currentformInstance->prop('mail');
 $data = $contactformsubmition->get_posted_data();
 //stop multiple submissions
 if ( ! session_id() ) {
 session_start();
 }
 $md5_data = md5(json_encode($data));
 if (isset($_SESSION['cf7_form_timer'.$md5_data])) {
 if ($_SESSION['cf7_form_timer'.$md5_data] > time()) {
 $data = array_pop($data);
 foreach ($data as $type => $d) {
 $result->invalidate($type, "You have already submitted a contact form, please wait at least 1 minutes before submitting another.");
 }
 } else {
 $_SESSION['cf7_form_timer'.$md5_data] = strtotime('+1 minute', time());
 }
 } else {
 $_SESSION['cf7_form_timer'.$md5_data] = strtotime('+1 minute', time());
 }
 
 return $result;
}
?>

I’ve retained some WPCF7 variable hooks that you might not need, but useful if you want to use this function for other checks against submitted values.

5 4 votes
Article Rating
Need some help? We are here for you!

We have a very friendly service - Come and chat to us and let us know what you need, we work for an hourly fee and can also provide you a no obligation quote and begin work immediately in most cases. Click "Request Support" or use our Live Chat.

Request support
Author: Dean WilliamsProfessional PHP Web Developer with expertise in OpenCart Web Development, WordPress Web Development, Bespoke Systems - also a seasoned Linux Server Administrator.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments


Share this page..

Let's begin your journey...

Get in touch us now to begin your journey to having a beautifully crafted mobile responsive website. If you're still unsure what we could do for you then take a look at our why us page which includes reviews, or take a look at our portfolio.

We can work on improving existing websites or craft a new stylish website making your business stand out.

You could always catch us by email, telephone, Skype or live chat to us below.


    0
    Would love your thoughts, please comment.x
    ()
    x