Reputation: 83
I’m using Contact Form 7 and its wpcf7_before_send_mail
action to interact with an API before sending the email. If the API returns an error, I want to be able to grab that error, display it as an error and prevent the form from being submitted.
I can’t seem to find what I’m looking for anywhere online. The best I can do is use the mail_sent_ok
message string and display the error within it (which is obviously not the solution).
Basically, the ultimate solution would be to force the form submission to fail.
Anyone else in the same boat?
Upvotes: 4
Views: 5236
Reputation: 2436
add_action('wpcf7_before_send_mail', 'your_function', 10, 3);
function your_function($form, &$abort, $object){
$error = 1;
/*if($error != 0) {
$abort = true;
$msgs = $form->prop('messages');
$msgs['mail_sent_ng'] = "An error happened"; //<- your custom error message
$form->set_properties(array('messages' => $msgs));
}*/
//echo "<pre>";
//print_r($_SERVER);
$current_site_ip_country_code = $_SERVER['HTTP_CF_IPCOUNTRY'];
$blocked_countries = array( 'CA', 'UK', 'RU', 'NL', 'CN', 'AC', 'AD', 'AE', 'AF', 'AT', 'BD', 'BR', 'CF', 'CG', 'DK', 'ES','IN' );
if ( in_array( $current_site_ip_country_code, $blocked_countries ) ) {
// echo "here";
// die();
$abort = true;
$msgs = $form->prop('messages');
$msgs['mail_sent_ng'] = "Not Allowed"; //<- your custom error message
$form->set_properties(array('messages' => $msgs));
}
}
Upvotes: 0
Reputation: 499
I don't know if someone might still need this. But you can use the below to do the job.
$submission = WPCF7_Submission::get_instance();
$submission->set_response( 'the message' )
Upvotes: 1
Reputation: 1060
Based on Louise-Philippe's answer, I solved with this code, since $object parameter happends to be always null:
add_action('wpcf7_before_send_mail', 'your_function', 10, 3);
function your_function($form, &$abort, $object){
$error = 1;
if($error != 0) {
$abort = true;
$msgs = $form->prop('messages');
$msgs['mail_sent_ng'] = "An error happened"; //<- your custom error message
$form->set_properties(array('messages' => $msgs));
}
}
Note: it's not mail_sent_ok
but mail_sent_ng
so you can have red border as predefined error messages.
Upvotes: 1
Reputation: 101
I'm not sure if it was possible at the time you asked your question but the wpcf7_before_send_mail hook has an abort flag that your just have to set to avoid mail to be sent.
for instance in pseudo PHP
add_action('wpcf7_before_send_mail', 'your_function', 10, 3);
function your_function($form, &$abort, $object){
$error = 1;
if($error != 0) {
$abort = true;
$object->set_response("An error happened");
}
}
Upvotes: 4
Reputation: 1153
you can do a custom validation with this filter :
add_filter("wpcf7_validate", function ($result, $tags) {
$valid = FALSE; // here you can do your API call to calculate the validation
if (!$valid) {
$result->offsetSet(
"reason"
, ["your-name" => "error message for this field"]
);
}
return $result;
}, 10, 2);
your-name
must be the name of a existing field of this form.
Upvotes: 0