0

I have a contact form 7 form that's sending information to an API. I want to display the API response on the front end.

I record the user information, then post it using wp_remote_post, with my example url. I want to be able to show the API response / $body on the front end. I have set it up to capture it in the debug log, but want to know if there is a way to post it to the frontend where my form is.

Normal Var Dump and print_r do not work as contact form 7 is an Ajax function.

add_action('wpcf7_mail_sent','Kiri_cf7_api_sender');

 function Kiri_cf7_api_sender ( $contact_form) {
     
    
    
     
     if ( $contact_form->title === 'Quote_form')
     {
        $submission = WPCF7_Submission::get_instance();

        if ($submission) 
        {
            $posted_data = $submission->get_posted_data();
            
            
            $name = $posted_data["your-name"];
            $surname = $posted_data["your-name2"];
            $phone = $posted_data["tel-922"];
            
            
            //example url//
            $url = www.mytestapi.com?$name&$surname&$phone;
            
            
            
            
            $response = wp_remote_post ($url);
            $body = wp_remote_retrieve_body( $response );
            
            
            
            

             
            
            
            
                     ob_start();                     // start buffer capture

    var_dump($name);
    var_dump($surname);
    var_dump($phone);
    
    
    
    
    

    $contents = ob_get_contents();  // put the buffer into a variable
    ob_end_clean();                 // end capture
    error_log($contents); 
            
             

            
            


        }
     }
 }

1 Answer 1

0

I have a contact form 7 form that's sending information to an API. I want to display the API response on the front end.

there are 2 ways to go about solving this type of problem,

1- Use the CF7 plugin's message response hook,

add_filter('wpcf7_display_message', 'my_response_msg',10,2);
function my_response_msg($message, $status){
    if('mail_sent_ok'==$status){
      $form = wpcf7_get_current_contact_form(); //in case you need the cf7 form object to validate....
      $message = 'Api response is: '. $api;
    }
  return $message;
}

however, keep in mind that you can only use text in this message, HTML markup will not work due to the limitations of the CF7 plugin reported here and here, and as you can appreciate, the functionality did exist, but the plugin author tends to bury his head in the sand on such type of issues.

2- if you need a richer interface, then you can either redirect the form submission process to another page on which you display your API response by storing it in a transient using similar logic to this answer...

or you can load a custom JavaScript on the form page which you trigger on the CF7 successful submission wpcf7mailsent event and then fetch the API request stored as a transient on the server using an AJAX request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.