How to Send Geolocation Through Webhooks

Introduction

Would you like to send geolocation through webhooks? It may become necessary to send through not only the address from your form submission but also the longitude and latitude. Using a small PHP snippet and the Webhooks addon, you can easily send through these details through and we’ll show you how!

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

If you need assistance with setting up the Webhooks addon, please check out this article.

To enable the Webhooks addon, from inside the form builder, navigate to Settings. Next, you click the Enable Webhooks toggle and you can begin to fill in your webhook URL, and then under the Request Body, which is the information you’ll send in the webhook, you can map over the fields from the 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 ) {
     
    if ( empty( $entry_id ) || empty( $form_data[ 'id' ] ) ) {
        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 location and assign the longitude and latitude to this webhook when the form is submitted.

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.