Last updated on October 18, 2023

WP Forms third-party integration

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Connect WP Forms with third-party services.

To integrate a third-party service with WP Forms, you can use the wpforms_process_complete hook to perform actions after a form submission. This allows you to send form data to an external API or perform any other custom action.

Here’s an example code snippet that demonstrates how to integrate WP Forms with a hypothetical third-party service called “MyAPI”:

/**
 * Send form data to MyAPI after WP Forms submission.
 *
 * @param array $fields Submitted form fields.
 * @param int   $entry_id Form entry ID.
 * @param array $form_data Form data.
 */
function wpsnippets_send_form_data_to_myapi( $fields, $entry_id, $form_data ) {
    // Prepare data to send to MyAPI.
    $data = array(
        'name'    => $fields['name'],
        'email'   => $fields['email'],
        'message' => $fields['message'],
    );

    // Convert data to JSON.
    $json_data = json_encode( $data );

    // Send data to MyAPI using cURL.
    $ch = curl_init( 'https://api.example.com/endpoint' );
    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $json_data );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen( $json_data ),
    ) );
    $response = curl_exec( $ch );
    curl_close( $ch );

    // Log the response for debugging purposes.
    error_log( 'MyAPI response: ' . $response );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_form_data_to_myapi', 10, 3 );

In this example, the wpsnippets_send_form_data_to_myapi function is hooked to the wpforms_process_complete action. It receives the submitted form fields, form entry ID, and form data as parameters. The function then prepares the data to be sent to MyAPI by extracting the relevant fields from the $fields array.

Next, the data is converted to JSON using

on_encode
. The cURL library is used to send a POST request to the MyAPI endpoint with the JSON data as the request body. The response from MyAPI is stored in the $response variable.

Finally, the response is logged using error_log for debugging purposes. You can modify this code snippet to suit your specific integration requirements by adjusting the data sent to MyAPI and handling the response accordingly.

Examples

Example 1: Integrating WP Forms with MailChimp

This use case demonstrates how to integrate WP Forms with MailChimp, a popular email marketing service. The code example shows how to add a custom function that hooks into the WP Forms submission process and sends the form data to a MailChimp list.

function wpsnippets_wpforms_mailchimp_integration( $fields, $entry, $form_data ) {
    // Get the form data
    $email = $fields['email']['value'];
    $name = $fields['name']['value'];

    // Prepare the data to be sent to MailChimp
    $data = array(
        'email' => $email,
        'name' => $name,
    );

    // Send the data to MailChimp
    $result = wp_remote_post( 'https://api.mailchimp.com/3.0/lists/{list_id}/members', array(
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode( 'user:apikey' ),
            'Content-Type' => 'application/json',
        ),
        'body' => json_encode( $data ),
    ) );

    // Handle the response from MailChimp
    if ( ! is_wp_error( $result ) && wp_remote_retrieve_response_code( $result ) === 200 ) {
        // Success
    } else {
        // Error
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_mailchimp_integration', 10, 3 );

This code example adds a custom function that hooks into the wpforms_process_complete action, which is triggered after a WP Forms submission is processed. The function retrieves the form data, prepares it to be sent to MailChimp, and uses the wp_remote_post function to make a POST request to the MailChimp API. The response from MailChimp is then handled accordingly.

Example 2: Integrating WP Forms with Salesforce

This use case demonstrates how to integrate WP Forms with Salesforce, a popular customer relationship management (CRM) platform. The code example shows how to add a custom function that hooks into the WP Forms submission process and sends the form data to Salesforce.

function wpsnippets_wpforms_salesforce_integration( $fields, $entry, $form_data ) {
    // Get the form data
    $email = $fields['email']['value'];
    $name = $fields['name']['value'];

    // Prepare the data to be sent to Salesforce
    $data = array(
        'Email' => $email,
        'FirstName' => $name,
    );

    // Send the data to Salesforce
    $result = wp_remote_post( 'https://example.salesforce.com/services/data/v52.0/sobjects/Lead', array(
        'headers' => array(
            'Authorization' => 'Bearer {access_token}',
            'Content-Type' => 'application/json',
        ),
        'body' => json_encode( $data ),
    ) );

    // Handle the response from Salesforce
    if ( ! is_wp_error( $result ) && wp_remote_retrieve_response_code( $result ) === 201 ) {
        // Success
    } else {
        // Error
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_salesforce_integration', 10, 3 );

This code example is similar to the previous one, but it sends the form data to Salesforce instead of MailChimp. The function hooks into the wpforms_process_complete action, retrieves the form data, prepares it to be sent to Salesforce, and uses the wp_remote_post function to make a POST request to the Salesforce API. The response from Salesforce is then handled accordingly.

Example 3: Integrating WP Forms with Google Sheets

This use case demonstrates how to integrate WP Forms with Google Sheets, a popular online spreadsheet tool. The code example shows how to add a custom function that hooks into the WP Forms submission process and sends the form data to a Google Sheets spreadsheet.

function wpsnippets_wpforms_google_sheets_integration( $fields, $entry, $form_data ) {
    // Get the form data
    $email = $fields['email']['value'];
    $name = $fields['name']['value'];

    // Prepare the data to be sent to Google Sheets
    $data = array(
        'Email' => $email,
        'Name' => $name,
    );

    // Send the data to Google Sheets
    $result = wp_remote_post( 'https://sheets.googleapis.com/v4/spreadsheets/{spreadsheet_id}/values/Sheet1!A1:D1:append', array(
        'headers' => array(
            'Authorization' => 'Bearer {access_token}',
            'Content-Type' => 'application/json',
        ),
        'body' => json_encode( array( 'values' => array( $data ) ) ),
    ) );

    // Handle the response from Google Sheets
    if ( ! is_wp_error( $result ) && wp_remote_retrieve_response_code( $result ) === 200 ) {
        // Success
    } else {
        // Error
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_google_sheets_integration', 10, 3 );

This code example demonstrates how to send WP Forms data to a Google Sheets spreadsheet. The function hooks into the wpforms_process_complete action, retrieves the form data, prepares it to be sent to Google Sheets, and uses the wp_remote_post function to make a POST request to the Google Sheets API. The response from Google Sheets is then handled accordingly.

Last updated on October 18, 2023. Originally posted on November 1, 2023.

Leave a Reply