Skip to content

Instantly share code, notes, and snippets.

@ovidiumght
Created July 3, 2022 17:59
Show Gist options
  • Select an option

  • Save ovidiumght/abb9e0528ebe9424bb926a050d3db89b to your computer and use it in GitHub Desktop.

Select an option

Save ovidiumght/abb9e0528ebe9424bb926a050d3db89b to your computer and use it in GitHub Desktop.
<?php
//send and display response data from an api or webhook
add_action('wpcf7_before_send_mail', 'send_to_api_webhook', 10, 3);
function send_to_api_webhook($contact_form, &$abort, $submission)
{
// replace your-subject with your form field
$get_input_user = $submission->get_posted_data('your-subject');
// replace abc with your api key
$the_key = 'abc';
// replace xyz with your api , webhook url, ex:
$url = 'xyz';
// prepare body json to send, can add many as you want. Replace keyword with the object from your api provider documentation.
$mybody = [
'keyword' => $get_input_user
];
// json_encode the body data
$goodbody = wp_json_encode($mybody);
// start to call, use POST or GET, depend on your project
$response = wp_remote_post($url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'blocking' => true,
'headers' => array(
// replace apikey with the object from your api provider documentation.
'apikey' => $the_key,
'content-type' => 'application/json'
),
'body' => $goodbody
)
);
// now get the response data
$response_data = $response['body'];
// jsondecode to get readable data
$to_use = json_decode($response_data, true);
// start get contact form 7 form properties
$properties = $contact_form->get_properties();
// display the response data form webhook, api, replae it with submit message of cf7. Replace point with with the object from your api provider documentation.
$properties['messages']['mail_sent_ok'] = $to_use['point'];
$contact_form->set_properties($properties);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment