Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom error message within wpcf7_before_send_mail

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?

like image 880
Yannick T. Avatar asked Oct 21 '25 14:10

Yannick T.


1 Answers

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");
  }
}
like image 193
Louis-Philippe Avatar answered Oct 23 '25 04:10

Louis-Philippe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!