I am stuck into a problem.

I sent contact form data through curl and I get a response from an API. How is it possible to display there results on my page? Ideally through AJAX and in my own custom div ?

add_action('wpcf7_mail_sent', function ($contact_form) { // Run code after the email has been sent 
  $title = $contact_form->title;
    $submission = WPCF7_Submission::get_instance();

    if ( $submission ) {
        $posted_data = $submission->get_posted_data();

        if ($posted_data['type'] == 'var1')





function APIcurl($var...)
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL =>  'https [...]

$response = curl_exec($curl);

How to show the response on my page ? I tried this but it was not successfull : document.addEventListener( 'wpcf7mailsent', function( event ) { alert(''); alert('test'); }, false ); }

Many thanks in advance

EDIT : maybe is the solution to add an "add_action" after the API function such as : add_action('wp_footer'... but I don't know how I can display the results of the API function in this.

  • try wpcf7submit instead of mail sent. – mrben522 Feb 21 at 17:12
  • Unfortunately it doesnt work also. :-/ – user161783 Feb 21 at 17:34
  • Your JS as written won't work - you would have to have it wrapped in <script> tags. Instead, try print_r($response) to see what response you're getting, and then you'll be able to parse however you wish. – WebElaine Feb 21 at 18:04
  • Thanks for your answer. I removed the script tags in my post, sorry :) If I put a print_r or somthing like echo 'test'; I cannot see the confirmation message of CF7. It displays only the arrows which are turning around. The thing is, how can be able to display the response where I want? – user161783 Feb 22 at 8:12
  • I tested also with this, but it occurs nothing: add_action('wp_footer', function() { ?> <!--API --> <script type="text/javascript"> document.addEventListener( 'wpcf7submit', function( event ) { alert('<?php /* print_r(json_decode(json_encode($response), true, 3));*/ ?>'); alert('message envoye'); <?php /* if ( '123' == event.detail.contactFormId ) { ga( 'send', 'event', 'Contact Form', 'submit' ); } */ ?> }, false ); </script> <?php }); – user161783 Feb 22 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.