Last updated on October 18, 2023

WP Forms webhooks integration

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

Integrate webhooks for custom actions in WP Forms.

To integrate WP Forms with webhooks in WordPress, you can use the wpforms_process_complete action hook along with a custom function. This allows you to send form data to an external service or perform any other action when a form is submitted.

Here’s an example code snippet that demonstrates how to achieve this integration:

function wpsnippets_wpforms_webhooks_integration( $entry, $form_data ) {
    // Get form data
    $form_id = $form_data['id'];
    $form_title = $form_data['title'];
    $form_fields = $form_data['fields'];

    // Prepare data to send to the webhook
    $data = array(
        'form_id' => $form_id,
        'form_title' => $form_title,
        'entry' => $entry,
        'form_fields' => $form_fields,
    );

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

    // Send data to the webhook URL
    $webhook_url = 'https://example.com/webhook-endpoint';
    $response = wp_remote_post( $webhook_url, array(
        'body' => $json_data,
        'headers' => array(
            'Content-Type' => 'application/json',
        ),
    ) );

    // Check if the webhook request was successful
    if ( is_wp_error( $response ) ) {
        // Handle error
        $error_message = $response->get_error_message();
        error_log( "Webhook request failed: $error_message" );
    } else {
        // Webhook request successful
        $response_code = wp_remote_retrieve_response_code( $response );
        $response_body = wp_remote_retrieve_body( $response );
        // Handle response
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_webhooks_integration', 10, 2 );

In this code snippet, we define a custom function wpsnippets_wpforms_webhooks_integration that is hooked to the wpforms_process_complete action. This function receives two parameters: $entry (the form entry data) and $form_data (the form data).

Inside the function, we extract the necessary information from the form data and prepare it to be sent to the webhook. We then use the wp_remote_post function to send the data as a JSON payload to the specified webhook URL.

After sending the webhook request, we check if it was successful using is_wp_error. If there was an error, we log it to the error log. If the request was successful, we can handle the response if needed.

This code snippet can be useful when you want to integrate WP Forms with external services or systems that accept data via webhooks. It allows you to automate processes or send form data to other applications for further processing.

Examples

Example 1: Sending form data to an external API

This use case demonstrates how to integrate WP Forms with an external API by sending form data as a webhook. The code example below shows how to create a custom function that hooks into the wpforms_process_complete action and sends the form data to an external API using the wp_remote_post() function.

function wpsnippets_send_form_data_to_api( $entry, $form_data ) {
    // Prepare the data to be sent
    $data = array(
        'name'  => $form_data['fields']['name']['value'],
        'email' => $form_data['fields']['email']['value'],
        // Add more fields as needed
    );

    // Send the data to the API
    wp_remote_post( 'https://api.example.com/endpoint', array(
        'body' => $data,
    ) );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_form_data_to_api', 10, 2 );

In this code example, we define a custom function wpsnippets_send_form_data_to_api() that takes two parameters: $entry and $form_data. Inside the function, we prepare the form data to be sent by extracting the relevant fields from the $form_data array. We then use the wp_remote_post() function to send the data to the specified API endpoint.

Example 2: Logging form submissions to a file

This use case demonstrates how to log WP Forms submissions to a file on the server. The code example below shows how to create a custom function that hooks into the wpforms_process_complete action and appends the form data to a log file using the file_put_contents() function.

function wpsnippets_log_form_submission( $entry, $form_data ) {
    // Prepare the data to be logged
    $log_data = date( 'Y-m-d H:i:s' ) . ' - ' . $form_data['fields']['name']['value'] . ' - ' . $form_data['fields']['email']['value'] . "n";

    // Append the data to the log file
    file_put_contents( '/path/to/log/file.txt', $log_data, FILE_APPEND );
}
add_action( 'wpforms_process_complete', 'wpsnippets_log_form_submission', 10, 2 );

In this code example, we define a custom function wpsnippets_log_form_submission() that takes two parameters: $entry and $form_data. Inside the function, we prepare the form data to be logged by concatenating the submission date, name, and email fields. We then use the file_put_contents() function to append the data to a log file specified by the file path.

Example 3: Sending form data to a third-party service

This use case demonstrates how to integrate WP Forms with a third-party service by sending form data as a webhook. The code example below shows how to create a custom function that hooks into the wpforms_process_complete action and sends the form data to a third-party service using the wp_remote_post() function.

function wpsnippets_send_form_data_to_service( $entry, $form_data ) {
    // Prepare the data to be sent
    $data = array(
        'name'  => $form_data['fields']['name']['value'],
        'email' => $form_data['fields']['email']['value'],
        // Add more fields as needed
    );

    // Send the data to the third-party service
    wp_remote_post( 'https://api.example.com/endpoint', array(
        'body' => $data,
    ) );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_form_data_to_service', 10, 2 );

In this code example, we define a custom function wpsnippets_send_form_data_to_service() that takes two parameters: $entry and $form_data. Inside the function, we prepare the form data to be sent by extracting the relevant fields from the $form_data array. We then use the wp_remote_post() function to send the data to the specified endpoint of the third-party service.

Last updated on October 18, 2023. Originally posted on January 8, 2024.

Leave a Reply