We often help our customers by writing small code snippets for them, so we will start to add snippets to this page in case other users might find these useful.
These code snippets can be added to your themes functions.php file or by using a Code Snippets plugin.
Modify Action values sent to API
This snippet modifies a certain value within an Action. In this case we are using the ‘woocommerce_order_status_changed’ action and we are modifying the ‘number’ field and also rounding the ‘subtotal’ field within each line item.
This can be used on any Action. To find the action name, look for it in the Actions Log and then simply change the ‘woocommerce_order_status_changed’ to whatever Action you are using.
function wpgetapi_modify_action_values( $args, $endpoint ) {
// set a custom value to 'number'
$args['number'] = '12345';
// round each line item subtotal
if( isset( $args['line_items'] ) ) {
foreach ( $args['line_items'] as $i => $line_item ) {
if( isset( $line_item['subtotal'] ) )
$args['line_items'][ $i ]['subtotal'] = round( $line_item['subtotal'], 2 );
}
}
return $args;
}
add_filter( 'wpgetapi_argument_values_before_woocommerce_order_status_changed', 'wpgetapi_modify_action_values', 10, 2 );
WPForms – send file to API
function wpgetapi_modify_final_args( $final_args, $wpgetapi ) {
// change '4' to the upload field ID within WPForms
$url = $wpgetapi->args['action_args']['fields']['4'];
$post_fields = array();
$boundary = wp_generate_password( 24, false );
$content_type = 'multipart/form-data; boundary=' . $boundary;
$payload = '';
// First, add the standard POST fields:
foreach ( $post_fields as $name => $value ) {
$payload .= '--' . $boundary;
$payload .= "\r\n";
$payload .= 'Content-Disposition: form-data; name="' . $name .
'"' . "\r\n\r\n";
$payload .= $value;
$payload .= "\r\n";
}
// Upload the file
if ( $url ) {
$payload .= '--' . $boundary;
$payload .= "\r\n";
$payload .= 'Content-Disposition: form-data; name="' . 'upload' .
'"; filename="' . basename( $url ) . '"' . "\r\n";
$payload .= "\r\n";
$payload .= file_get_contents( $url );
$payload .= "\r\n";
}
$payload .= '--' . $boundary . '--';
$final_args['httpversion'] = '1.1';
$final_args['body'] = $payload;
$final_args['headers']['content-type'] = $content_type;
return $final_args;
}
add_filter( 'wpgetapi_final_request_args', 'wpgetapi_modify_final_args', 10, 2 );
Elementor Forms – sending data to API in the body
This snippet allows you to send data from your Elementor Forms to an API. The data is being sent in the body and we are using the body_variables argument, which is available in the Pro Plugin extension.
function wpgetapi_send_to_api_after_submit( $record, $handler ) {
$form_name = $record->get_form_settings( 'form_name' );
// Replace MY_FORM_NAME with the name you gave your form
if ( 'MY_FORM_NAME' !== $form_name ) {
return;
}
// get the fields and their data
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
// the data we are sending
$body = array(
'Type' => 'Test',
'Id' => 'XC123456',
'Data' => array(
array(
'id' => 'unique_ref',
'value' => $fields['unique_ref']
),
array(
'id' => 'first_name',
'value' => $fields['first_name']
),
array(
'id' => 'last_name',
'value' => $fields['last_name']
),
)
);
// call the API using the wpgetapi_endpoint() template tag
$data = wpgetapi_endpoint( 'MY_API_ID', 'MY_ENDPOINT_ID',
array(
'debug' => false,
'body_variables' => $body,
)
);
var_dump($data);
}
add_action( 'elementor_pro/forms/new_record', 'wpgetapi_send_to_api_after_submit', 10, 2 );
Elementor Forms – sending data to API in the query string
This snippet allows you to send data from your Elementor Forms to an API. The data is being sent in the query string (the URL) and we are using the query_variables argument, which is available in the Pro Plugin extension.
function wpgetapi_send_to_api_after_submit( $record, $handler ) {
$form_name = $record->get_form_settings( 'form_name' );
// Replace MY_FORM_NAME with the name you gave your form
if ( 'MY_FORM_NAME' !== $form_name ) {
return;
}
// get the fields and their data
$raw_fields = $record->get( 'fields' );
$fields = [];
foreach ( $raw_fields as $id => $field ) {
$fields[ $id ] = $field['value'];
}
// the data we are sending
$license_field = $fields['license-field'];
// call the API using the wpgetapi_endpoint() template tag
$data = wpgetapi_endpoint( 'MY_API_ID', 'MY_ENDPOINT_ID',
array(
'debug' => false,
'query_variables' => 'license=' . $license_field,
)
);
var_dump($data);
}
add_action( 'elementor_pro/forms/new_record', 'wpgetapi_send_to_api_after_submit', 10, 2 );
Contact Form 7 – modify field values with data from API
This snippet gets price data from Binance for ADA and ALGO cryptocurrencies and adds their price to the Contact Form 7 fields that are named ‘price-ada’ and ‘price-algo’. It also generates a 7 digit random number and adds this number to the ‘random-number’ field.
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
if( ! isset( $array['price-ada'] ) || ! isset( $array['price-algo'] ) )
return $array;
// the symbols
$symbols = '["ADAUSDT","ALGOUSDT"]';
// call the binance api
$prices = wpgetapi_endpoint( 'binance', 'price',
array(
'debug' => false,
'query_variables' => 'symbols=' . urlencode( $symbols ),
)
);
if( $prices ) {
$array['random-number'] = rand(1000000, 9999999);
foreach ( $prices as $key => $price ) {
if( $price['symbol'] == 'ADAUSDT' )
$array['price-ada'] = $price['price'];
if( $price['symbol'] == 'ALGOUSDT' )
$array['price-algo'] = $price['price'];
}
}
return $array;
};
add_filter( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1 );
Redirect after receiving response from API
function wpgetapi_redirect_after_response( $response, $response_code, $api ) {
// make sure we only do it for the API and endpoint we want
// change 'quote' and 'random' to suit
if( $api->api_id != 'quote' && $api->endpoint_id != 'random' )
return $response;
// if successfull 200 response, do a redirect
if( $response_code == 200 ) {
$url = home_url( '/confirmation' );
wp_redirect( $url );
exit;
}
// if failed 400 response, do a redirect
if( $response_code == 400 ) {
$url = home_url( '/error' );
wp_redirect( $url );
exit;
}
return $response;
}
add_filter( 'wpgetapi_before_retrieve_body', 'wpgetapi_redirect_after_response', 9999, 3 );
Modify the API timeout time
function wpgetapi_set_request_args_parameters( $args ) {
$args['timeout'] = 30;
return $args;
}
add_filter( 'wpgetapi_default_request_args_parameters', 'wpgetapi_set_request_args_parameters' );