Last active
June 9, 2021 14:39
-
-
Save Shelob9/94bb4ff5480ee01fb93f3e24123fa73d to your computer and use it in GitHub Desktop.
Example Caldera Forms Proccesor See: https://calderaforms.com/doc/getting-submission-data-in-a-form-processor/
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 | |
/** | |
Plugin Name: Caldera Forms Add-on For Remote API | |
Description: Example Caldera Forms processor to send data to a remote API | |
*/ | |
/** | |
* Register processor | |
*/ | |
add_filter( 'caldera_forms_get_form_processors', function( $processors ) { | |
$processors['my_processor_send_to_api'] = array( | |
'name' => 'Remote API', | |
'description' => 'Send Caldera Forms Data To Remove API', | |
'pre_processor' => 'my_processor_send_to_api_pre_process' | |
); | |
return $processors; | |
} ); | |
/** | |
* Process submission | |
* | |
* @param array $config Processor config | |
* @param array $form Form config | |
* @param string $process_id Unique process ID for this submission | |
* | |
* @return void|array | |
*/ | |
function my_processor_send_to_api_pre_process( $config, $form, $process_id ){ | |
//get all form data | |
$data = Caldera_Forms::get_submission_data( $form ); | |
$response = wp_remote_post( 'https://service.com/api/something', array( | |
'body' => $data | |
)); | |
//If API responds with success return void | |
if( 200 == wp_remote_retrieve_response_code( $response ) || 201 == wp_remote_retrieve_response_code( $response ) ){ | |
return; | |
} | |
//find and return error | |
if( is_wp_error( $response ) ){ | |
$error = $response->get_error_message(); | |
}elseif ( isset( $response[ 'error' ]) ){ | |
$error = $response[ 'error' ]; | |
}else{ | |
$error = 'Something bad happened'; | |
} | |
//returning in pre-precess stops submission processing. | |
return array( | |
'note' => $error, | |
'type' => 'error' | |
); | |
} |
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 | |
/** | |
Plugin Name: Caldera Forms Add-on For Remote API | |
Description: Example Caldera Forms processor to send data to a remote API | |
*/ | |
/** | |
* Register processor | |
*/ | |
add_filter( 'caldera_forms_get_form_processors', function( $processors ) { | |
$processors['my_processor_send_to_api'] = array( | |
'name' => 'Remote API', | |
'description' => 'Send Caldera Forms Data To Remove API', | |
'pre_processor' => 'my_processor_send_to_api_pre_process' | |
); | |
return $processors; | |
} ); | |
/** | |
* Process submission | |
* | |
* @param array $config Processor config | |
* @param array $form Form config | |
* @param string $process_id Unique process ID for this submission | |
* | |
* @return void|array | |
*/ | |
function my_processor_send_to_api_pre_process( $config, $form, $process_id ){ | |
//get all form data | |
$data = Caldera_Forms::get_submission_data( $form ); | |
//Collect data by field slug | |
$data_by_slug = array(); | |
foreach ( $data as $field_id => $value ){ | |
$field = Caldera_Forms_Field_Util::get_field( $field_id, $form ); | |
$slug = $field[ 'slug' ]; | |
$data_by_slug[ $slug ] = $value; | |
} | |
$response = wp_remote_post( 'https://service.com/api/something', array( | |
'body' => $data_by_slug | |
)); | |
//If API responds with success return void | |
if( 200 == wp_remote_retrieve_response_code( $response ) || 201 == wp_remote_retrieve_response_code( $response ) ){ | |
return; | |
} | |
//find and return error | |
if( is_wp_error( $response ) ){ | |
$error = $response->get_error_message(); | |
}elseif ( isset( $response[ 'error' ]) ){ | |
$error = $response[ 'error' ]; | |
}else{ | |
$error = 'Something bad happened'; | |
} | |
//returning in pre-precess stops submission processing. | |
return array( | |
'note' => $error, | |
'type' => 'error' | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment