[Forminator Geolocation Add-on] get the exact coordinates from the email notification

for forminator pro, I installed google map geocoding api. and now customer can drop pin the exact coordinates on google map. However when i receive the email notifiation, I only receive the address but not coordinnates. How I can get the exact corrdinates via email notification?

  • Adam
    • Support Gorilla

    Hi tetsuo fujisawa

    I hope you’re well today!

    The Geolocation Add-on main purpose is for help with auto-completion of address and it works with address field. As for raw coordinates, I’ve asked our Second Line Support team to look into it and see if we would be able to provide some workaround to achieve that – but they need to check first.

    I’d appreciate some patience as they are dealing with a lot of complex tasks on daily basis. We’ll update you here as soon as they check it and if it would be doable, we’ll let you know how to do it exactly.

    Kind regards,
    Adam

  • tetsuo fujisawa
    • New Recruit

    Hi Adam,

    Thanks for the update. I’m glad to hear that Second Line Support is looking into a workaround for using raw coordinates with the Geolocation Add-on. I appreciate your patience and understanding while they investigate.

    I’ll keep an eye on this thread for updates

  • Prashant
    • Staff

    Hi tetsuo fujisawa

    I hope you are doing well.

    We have worked on your request and found a solution. Please add a must-use plugin to your site’s wp-content/mu-plugins folder like this https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins, then add the following code to the plugin’s php file:

    <?php
    add_action('wp_footer', 'wpmudev_get_geolocation_mail', 9999);
    function wpmudev_get_geolocation_mail() {
        global $post;
    	if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
    		return;
    	}
        ?>
        <script type="text/javascript">
        jQuery(document).ready(function($){
            $(document).on('forminator:datachange forminator:autosuggest:datachange forminator:set_current_location forminator:set_default_location', function(e){
                var lat = $(e.target).attr('data-lat');
                var lng = $(e.target).attr('data-lng');
                $('input[name="hidden-1"]').val(lat + ', ' + lng);
            });
        });
        </script>
        <?php
    }
    
    add_filter( 'forminator_prepared_data', 'wpmudev_update_hidden_field_val', 10, 2 );
    function wpmudev_update_hidden_field_val( $prepared_data, $module_object ){
        $form_ids = array(6); //Please change the form ID
    	if ( !in_array( $module_object->id, $form_ids ) ) {
    		return $prepared_data;
    	}
        
        foreach ( $prepared_data as $key => $value ) {
            if ( strpos( $key, 'hidden-1' ) !== false ) {
                $prepared_data[$key] = sanitize_text_field( $_POST[$key] );
            }
        }
    
    	return $prepared_data;
    }
    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_field_data', 10, 3 );
    function wpmudev_change_hidden_field_data( $entry, $module_id, $field_data_array ) {
        $form_ids = array(6); //Please change the form ID
         if ( !in_array( $module_id, $form_ids ) ) {
    		return;
          }
        
        foreach ( $field_data_array as $key => $value ) {
            if ( strpos( $value['name'], 'hidden-1' ) !== false ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( $_POST[$value['name']] );
            }
        }
    }

    Also, in the form please add a hidden field just like the attached image:

    [attachments are only viewable by logged-in members]

    Note: In the code please change 6 to your form’s ID and hidden-1 to your hidden field’s ID.

    We recommend to test this on the dev/staging version first before putting it on the live site.

    Hope it will solve your problem.

    Kind Regards
    Prashant

  • Mary Jackson
    • New Recruit

    Greetings, I have a different reason I’m interested in getting lat/long in a field form (for a digital media project) I have the php working, and an email sends a form that has lat long, but I’m a little lost on the

    “Note: In the code please change 6 to your form’s ID and hidden-1 to your hidden field’s ID.” part.
    Right now the form I’m using is ID 20. The hidden one is already designated -1, apologies if I’m missing something. Where would “the code” be located? Thank you!

    Mary