I need Contact Form 7 to send one email when validation has been failed. Then send the normal CF7 email when the form is submitted correctly. Ridiculous I know but clients!
I think I’m relatively close with the following:
function send_failed_vaildation_email( $data ) {
$messagesend = 'Name:' . $_POST['your-name'];
$messagesend .= 'rnEmail:' .$_POST['email'];
$messagesend .= 'rnPhone:' .$_POST['validPhone'];
$messagesend .= 'rnRate:' .$_POST['rate'];
$messagesend .= 'rnBased:' .$_POST['based'];
wp_mail('c******y@gmail.com', 'failed validation mail', $messagesend );
}
add_filter("wpcf7_posted_data", "send_failed_vaildation_email");
However this sends all submissions regardless if they pass or fail the validation.
wpcf7_before_send_mail
is no good as it only fires once the submission passes validation.
I’m either looking for a different hook to use instead of wpcf7_posted_data
that only fires when validation is failed or an if statement I can place around wp_mail
for the same effect.
Thanks in advance
This is how I managed to get it working.
@rnevius answer was failing because the
wpcf7_posted_data
filter is applied before the data is validated so nothing is invalid at that time.Combining @rnevius’ answer with the
wpcf7_submit
filter and it works as expected.Full code:
This hasn’t been tested, but I’m pretty sure you’re going to need to hook into the get_invalid_fields() instance. Something like:
You can see the form submission process in the plugin trac.
EDIT: I also just noticed the
'wpcf7_validation_error'
hook in contact-form.php…That may be all you need, as it only fires when there’s an error.UPDATED CODE FOR LATEST CONTACT FORM 7