Home / Admin / Sending Geolocation Data Through Webhooks
Duplicate Snippet

Embed Snippet on Your Site

Sending Geolocation Data Through Webhooks

This snippet works with the Geolocation and Webhooks addon. It allows you to send geolocation data to your webhook endpoint through WPForms.

<10
Code Preview
php
<?php
/**
 * 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 );

Comments

Add a Comment