Reputation: 121
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 .= '\r\nEmail:' .$_POST['email'];
$messagesend .= '\r\nPhone:' .$_POST['validPhone'];
$messagesend .= '\r\nRate:' .$_POST['rate'];
$messagesend .= '\r\nBased:' .$_POST['based'];
wp_mail('c******[email protected]', '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
Upvotes: 4
Views: 3718
Reputation: 416
UPDATED CODE FOR LATEST CONTACT FORM 7
function is_gmail($email) {
if(substr($email, -10) == '@gmail.com') {
return true;
} else {
return false;
};
};
function custom_email_validation_filter($result, $tag) {
$tag = new WPCF7_Shortcode( $tag );
if ( 'your-email' == $tag->name ) {
$the_value = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';
if(!is_gmail($the_value)){
$result->invalidate( $tag, "Are you sure this is the correct address?" );
};
};
return $result;
};
add_filter('wpcf7_validate_email','custom_email_validation_filter', 20, 2); // Email field
add_filter('wpcf7_validate_email*', 'custom_email_validation_filter', 20, 2); // Required Em
Upvotes: 0
Reputation: 121
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:
function send_failed_vaildation_email() {
$submission = WPCF7_Submission::get_instance();
$invalid_fields = $submission->get_invalid_fields();
$posted_data = $submission->get_posted_data();
if ( !empty( $invalid_fields ) ) {
$messagesend = 'Name:' . $posted_data['your-name'];
$messagesend .= '\r\nEmail:' . $posted_data['email'];
$messagesend .= '\r\nPhone:' . $posted_data['validPhone'];
$messagesend .= '\r\nRate:' . $posted_data['rate'];
$messagesend .= '\r\nBased:' . $posted_data['based'];
$messagesend .= count($invalid_fields);
wp_mail('c*******[email protected]', 'failed validation mail', $messagesend );
}
}
add_filter("wpcf7_submit", "send_failed_vaildation_email");
Upvotes: 4
Reputation: 27092
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:
function send_failed_vaildation_email() {
$submission = WPCF7_Submission::get_instance();
$invalid_fields = $submission->get_invalid_fields();
$posted_data = $submission->get_posted_data();
if ( !empty( $invalid_fields ) ) {
$messagesend = 'Name:' . $posted_data['your-name'];
$messagesend .= '\r\nEmail:' . $posted_data['email'];
$messagesend .= '\r\nPhone:' . $posted_data['validPhone'];
$messagesend .= '\r\nRate:' . $posted_data['rate'];
$messagesend .= '\r\nBased:' . $posted_data['based'];
wp_mail('c******[email protected]', 'failed validation mail', $messagesend );
}
}
add_filter("wpcf7_posted_data", "send_failed_vaildation_email");
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.
Upvotes: 1