Skip to content
  • Why WPGetAPI?
  • Examples
  • Pricing
  • Plugins
    • PRO Plugin
    • API to Posts Plugin
    • OAuth 2.0 Plugin
  • Docs
  • Support
    • Frequently Asked Questions
    • Feature Request
    • Support Ticket
  • account_circle
PRO Plugin
Howdy! How can we help you?
  • Introduction

    • Quick Start
    • Frequently Asked Questions
    • Will this work with my API?
    • Understanding API docs
    • Step by Step Example
  • Setup

    • Setup Page
    • Endpoint Page
    • Parameters - Query String
    • Parameters - Headers
    • Parameters - Body POST
    • Shortcode
    • Template Tag
    • Gutenberg Block
  • Connecting your API

    • API Key in Headers
    • API Key in Query String
    • Authentication & Authorization
  • Output API Data

    • Format API to HTML
    • Format API to HTML Table
    • Format API to Image Gallery
    • Format JSON Data
  • Integrations

    • Send form data to API
    • WPForms to API
    • Gravity Forms to API
    • Contact Form 7 to API
    • WooCommerce to API
    • WooCommerce API Product Sync
    • wpDataTables to API
    • Connect WordPress to OpenAI
    • Connect RapidAPI to WordPress
    • Connect Zoho CRM to WordPress
    • Page Builders (Elementor, DIVI)
    • Formidable Forms to API
    • Elementor Form to API
    • JetFormBuilder to API
    • Fluent Forms to API
    • WS Form to API
    • Ninja Tables to API
    • Easy Digital Downloads(EDD) API Product Sync
    • Ultimate Member Forms to API
  • Tips & Tricks

    • Filters & Action Hooks
    • Code Snippets Plugin
    • Troubleshooting
    • Code Snippets
  • PRO Plugin

    • Installation
    • Actions
    • Tokens
    • Caching
    • Nested data
    • Chaining API calls
    • Dynamic Variables
    • Format API to HTML
    • Call API on user registration
    • Using AJAX to trigger API call
    • Base64 Encoding
    • Licensing
    • Filters & Action Hooks
  • OAuth 2.0 Plugin

    • Installation
    • How it works
    • Grant Type - Client Credentials
    • Grant Type - Authorization Code
    • Licensing
  • API to Posts Plugin

    • Installation
    • Getting Started
    • Multiple Endpoints
    • Mapping Fields
    • Filters & Action Hooks
    • Pagination

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' );
On this page
contact_support

Still not sure?

APIs can be daunting and sometimes downright confusing.

But we are here to help! Our support is amazing and we can assist you with any setup required and ensure your API integrations are a success!

Ask Us Anything

Connect your WordPress website to external APIs without writing a single line of code. Send data from WordPress to your API or display data from your API within WordPress.

Support

  • Documentation
  • Support Ticket
  • Refund Policy
  • Contact Us
  • About Us
  • Affiliates

Articles

  • Quick Start Guide
  • WooCommerce Orders to API
  • WooCommerce API Products Sync
  • Contact Form 7 to API
  • WPForms to API
  • Gravity Forms to API
  • WordPress to RapidAPI

WordPress API Plugins

  • Free WPGetAPI Plugin
  • PRO Plugin
  • API to Posts Plugin Plugin
  • OAuth 2.0 Authorization Plugin

© 2025 WPGetAPI. All Rights Reserved.

"*" indicates required fields

Name*
This field is for validation purposes and should be left unchanged.