Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.