Last active
January 31, 2018 18:30
-
-
Save Shelob9/7309e5b3e505caef56110d62a898c989 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Callback function for Caldera Forms Run Action to send to a remote API | |
* | |
* SEE: https://calderaforms.com/downloads/caldera-forms-run-action/ | |
*/ | |
add_filter( 'my_run_action', 'my_run_action_callback' ); | |
function my_run_action_callback( $data ) { | |
//Create request body | |
$request_data = array( | |
//Add each part of array here | |
'MODULE' => $data[ 'module' ], | |
'EMAIL' => $data[ 'email' ] | |
); | |
//Send request - SEE: https://developer.wordpress.org/reference/functions/wp_remote_post/ | |
//IMPORTANT change URL to your API URL | |
$r = wp_remote_post( 'https://google.com', | |
array( | |
//You might need to json_encode this and set the content-type, check the API docsss | |
'body' => $request_data | |
) | |
); | |
//Request failed without response | |
if( is_wp_error( $r ) ){ | |
//Cause form submission to fail with the WP_Error message displayed | |
return array( | |
'type' => 'error', | |
'message' => $r->get_error_message() | |
); | |
} | |
//Request failed with response | |
//IMPORTANT: Check your API docs for what status code(s) indicate sucess | |
if( 201 !== wp_remote_retrieve_response_code( $r ) ){ | |
//Cause form submission to fail with some message display | |
return array( | |
'type' => 'error', | |
//Your API response might have errors, use $body = wp_remote_retrieve_body($r) to read request body | |
'message' => 'Failed with code ' . wp_remote_retrieve_response_code( $r ) | |
); | |
} | |
//Don't return anything because it worked! | |
//Returning in a pre-process callback causes form submission to stop | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment