(Wordpress) Contact form 7 abort filter before_send_mail

I use contact form 7 as the mail system on my WordPress website. I use the wpcf7_before_send_mail filter to send all data to an external web service (SOAP). When I receive the β€œSUCCESS” message of this web service, everything should continue as usual, but when I receive the message β€œFAILED”, contact form 7 should not send an email message and another output message should appear on the website. Is it possible to change this inside a function?

<? add_filter( 'wpcf7_load_js', '__return_false' ); add_filter( 'wpcf7_load_css', '__return_false' ); add_action('wpcf7_before_send_mail', 'wpcf7_soap_service'); //Pushen via SOAP service naar servers function wpcf7_soap_service($contact_form) { $submission = WPCF7_Submission::get_instance(); if ( $submission ) { /*** POST variabelen ***/ $posted_data = $submission->get_posted_data(); /*** SOAP settings ***/ ini_set("soap.wsdl_cache_enabled", "0"); /*** variabelen opzetten ***/ define('BROADCAST_URL','XXX'); define('SIM_LOGIN', 'XXX'); define('SIM_PASSWORD', 'XXX'); define('ENV_KEY', 'XXX'); /*** login parameters ***/ $params = array( 'username' => SIM_LOGIN, 'password' => SIM_PASSWORD, 'environmentKey' => ENV_KEY, ); /*** client opzetten ***/ $client = new SoapClient( BROADCAST_URL, array( 'soap_version' => SOAP_1_1 ) ); /*** Parameters ***/ $address["box"] = $posted_data["box"]; $address["country"] = $posted_data["country"]; $address["number"] = $posted_data["streetnumber"]; $address["postalcode"] = $posted_data["postalcode"]; $address["street"] = $posted_data["street"]; $address["town"] = $posted_data["town"]; $birthdate = $posted_data["birthdate"] . "T00:00:00"; $email = $posted_data["email"]; $firstname = $posted_data["firstname"]; $lastname = $posted_data["lastname"]; $phone = $posted_data["phone"]; /*** STDClass aanmaken met gevraagde data ***/ $std = new stdClass(); $std->Firstname = $firstname; $std->Lastname = $lastname; $std->Birthdate = $birthdate; $std->Phone = $phone; $std->Email = $email; $std->Address = new stdClass(); $std->Address->Street = $address["street"]; $std->Address->Number = $address["number"]; $std->Address->Box = $address["box"]; $std->Address->PostalCode = $address["postalcode"]; $std->Address->Town = $address["town"]; $std->Address->Country = $address["country"]; if(!empty($_FILES['cv'])){ $std->Files = new stdClass(); $std->Files->File["FileName"] = $_FILES["cv"]["name"]; $std->Files->File["DataFile"] = base64_encode($_FILES["cv"]["tmp_name"]); $std->Files->File["FileType"] = "CV"; } /*** Functie OpenSession ***/ try{ $token = $client->OpenSession($params); }catch(SoapFault $ex){ // ABORT OVER HERE } $token = $token->OpenSessionResult; /*** Functie AddApplication ***/ try{ $result = $client->AddApplication(array("token" => $token, "application" => $std)); }catch(SoapFault $ex){ // ABORT OVER HERE } if($result->AddApplicationResult->Status == "Success"){ // ABORT OVER HERE } /*** Functie CloseSession ***/ try{ $app = $client->CloseSession($token); }catch(SoapFault $ex){ // ABORT OVER HERE } } } 
+5
source share
1 answer

You can skip mail using:

 add_filter( 'wpcf7_skip_mail', '__return_true' ); 

Since you disabled javascript with

 add_filter( 'wpcf7_load_js', '__return_false' ); 

then you can use:

 add_filter( 'wpcf7_form_response_output', 'wpse_form_response_output', 10, 4 ); 

in your wpcf7_before_send_mail action wpcf7_before_send_mail , where is your custom error response:

 function wpse_form_response_output( $output, $class, $content, $object ) { return sprintf( '<div class="wpcf7-response-output wpcf7-display-none wpcf7-mail-sent-ng" role="alert" style="display: block;">%s</div>', __( 'SOAP ERROR - Mail not sent!' ) ); } 
+4
source

Source: https://habr.com/ru/post/1232003/


All Articles