WebDesires - Web Development Cannock, West Midlands
Basket is empty

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

There is a mobile optimized version of this page, view AMP Version.

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.

Here is our final solution to stop duplicate form submissions in Contact From 7, 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()) {
			echo '{"mailSent":false,"status":"validation_failed","into":"#'.$data['_wpcf7_unit_tag'].'","message":"You have already submitted a contact form, please wait at least 1 minutes before submitting another.","invalidFields":[{"into":"span.wpcf7-form-control-wrap.captcha-170","message":"You have already submitted a contact form, please wait at least 1 minutes before submitting another.","idref":null}]}';
			die();
		} else {
			$_SESSION['cf7_form_timer'] = strtotime('+1 minute', time());
		}
	} else {
		$_SESSION['cf7_form_timer'] = 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.

0 0 vote
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

SHARING IS CARING!
Facebooktwitterredditpinterestlinkedinmail

Author: Dean Williams

Professional PHP Web Developer with expertise in OpenCart Web Development, WordPress Web Development, Bespoke Systems - also a seasoned Linux Server Administrator.


0 Comments
Inline Feedbacks
View all comments

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 E-Mail, Telephone, Skype or Live Chat to us below.


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