How to Send Geolocation Through Webhooks

Introduction

Would you like to send geolocation through webhooks? Geolocation data can be a valuable addition to your webhook payload, providing location-specific information about your form submissions. In this tutorial, we’ll guide you through the process of including geolocation data in your webhooks using a PHP snippet.

Creating the form

First, you’ll need to create a form and enable geolocation. If you need help creating a form with geolocation enabled, please review this documentation.

create your form and enable autocomplete and display map on the address field to send geolocation values through webhooks

Setting up the webhook

To get started, create a form and ensure that geolocation is enabled. If you require assistance with the process of enabling geolocation in your form, refer to this documentation for guidance.

To activate the Webhooks addon, access your form builder and proceed to the Settings section. There, toggle on the Enable Webhooks option. You can then proceed to input your webhook URL. In the Request Body section, you will define the information you intend to send via the webhook, including mapping the relevant fields from your form.

map your form fields to your variable names and enter the webhook URL.

For the purpose of this tutorial, we’re using the Webhook.site for testing that our information is being sent through our webhook. When you’re setting up your webhook, you would use the webhook URL to the external source you’re posting to and assign your variables. It’s important to remember that each external source that accepts incoming webhooks may have different ways of setting up variables to send the information. You’ll need to research this external source to make sure you don’t need to manually create your variables inside that source to make sure the variable names match what you’re assigning in the form builder.

Adding the snippet

Now it’s time to add the snippet. For any assistance on how and where to add snippets to your site, please see this tutorial.

/**
 * Send geolocation through webhooks.
 *
 * @link https://wpforms.com/developers/how-to-send-geolocation-through-webhooks
 */
 
function wpf_dev_geolocation_webhook( $options, $webhook_data, $fields, $form_data, $entry_id ) {
     
    // Optional, you can limit to specific forms. Below, we restrict output to
    // form #1899.
    if ( absint( $form_data[ 'id' ] ) !== 1899 ) {
        return $options;
    }
     
    $body = ! is_array( $options[ 'body' ] ) ? json_decode( $options[ 'body' ], true ) : $options[ 'body' ];
     
    $location = wpforms()->entry_meta->get_meta(
            [
                'entry_id' => $entry_id,
                'type'     => 'location',
                'number'   => 1,
            ]
    );
     
    if ( ! empty( $location[0]->data ) ) {
        $body[ 'location' ] = $location[0]->data;
    }
     
    $options[ 'body' ] = wp_json_encode( $body );
     
    return $options;
     
    }
  
add_filter( 'wpforms_webhooks_process_delivery_request_options', 'wpf_dev_geolocation_webhook', 10, 5 );

With this snippet, we’re going to add an additional variable to the webhook called entry_id and assign this ID number that is created from WPForms when the form is submitted but only for the form ID 1899, you’ll need to update this form ID number to match your own ID. If you’re not sure where to find your form ID number, please review this tutorial.

Now when you see the information sent from the webhook, you’ll notice not only your mapped field names are there, but also the location as well.

with this snippet you can send geolocation through webhooks

And that’s all you need to send the location value using a webhook. Would you like to also send through field values from a Checkbox, Dropdown or Multiple Choice? Take a look at our tutorial on How to Send Field Values with Webhooks.