How to Send Geolocation Through Webhooks

Overview

Would you like to send geolocation details through the WPForms Webhooks addon? It may become important to send all the data from the form to your external source using webhooks including the location. In this tutorial, we’ll show you how to send geolocation values using a webhook.

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

Adding the snippet to send geolocation value through a webhook

Now it’s time to add the snippet. If you need any help in how to add a snippet to your site, please review this tutorial.

/**
 * Send location through using webhook.
 *
 * @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 );

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.