Submit Contact Form 7 data to External API

  • Question
    Anonymous
    Inactive
    add_action( 'wpcf7_before_send_mail','blog_cf7_api_sender' );  function blog_cf7_api_sender( $contact_form ) { $form_id = ( $contact_form )->id();      if ( $form_id == '02d6ca7') {          $submission = WPCF7_Submission::get_instance();                if ( $submission ) {             $posted_data = $submission->get_current();                  $name = $posted_data['client-name'];             $phone = $posted_data['client-phone'];             $stream_code = [4yv1];             $token = [12345];                  $url="https://coolapi.com";                          $args = array(              'body' => json_encode(array(                  'name' => $name,                  'phone' => $phone,                  'stream_code' => $stream_code,                  'token' => $token,                  )                  'headers' => array(                  'Authorization' => 'Bearer $token',                  'Content-Type' => 'application/json',                 )                            );                  $response = wp_remote_post( $url, $args );        }     }  }  

    I have created a custom plugin within WordPress to POST contact form 7 data to an external API when a client submits their info, I followed a tutorial on YouTube step by step, but nothing is being posted to the API URL. I am not sure if it’s because the coding for contact form 7 is outdated now or if I am possibly using wp_remote_post incorrectly.

    The hook is OK, but it is only executed when sending e-mails is activated on the form.

    I would recommend that you try to find out what the problem is by debugging. Add test outputs like

    echo "aaaa";exit;

    in different places (vary the letters). Also let var_dump() output what is in $form_id and in $response. This way you should be able to find out what’s wrong.

  • You must be logged in to reply to this topic.