Skip Navigation

[Resolved] Extracting Latitude and Longitude to a custom field when add/edit post in admin

This thread is resolved. Here is a description of the problem and solution.

Problem:
Extracting Latitude and Longitude to a custom field when add/edit post in admin

Solution:
To extract the lat,long from custom address field and copy it to different custom field when you add/edit post from backend admin, you will require to use standard WordPress hook save_post.

You can find proposed solution in this case with the following reply:
https://toolset.com/forums/topic/extracting-latitude-and-longitude-to-a-custom-field-2/#post-2323719

Relevant Documentation:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

0% of people find this useful.

This support ticket is created 3 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by caseyM-2 3 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#2323457

I am building a directory site and need the latitude and longitude in a field that I can reference.

I have read this post https://toolset.com/forums/topic/extracting-latitude-and-longitude-to-a-custom-field/ and I am trying to follow his instructions.

The custom post type is after-death-service
The map field is business-address
The field I want to put the co-ordinates into is business-location

I added the following code to a custom code snippet:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

error_log("code is running...");
add_action( 'save_post_after-death-service', 'ts_save_coordinates' );
function ts_save_coordinates( $post_ID ){

error_log("action callback called...");
error_log("$_POST");
    if ( isset($_POST['toolset-extended-form-wpcf']['business-address']) )
    {
      	error_log("condition met");
        $lat = $_POST['toolset-extended-form-wpcf']['business-address']['latitude'];
        $lon = $_POST['toolset-extended-form-wpcf']['business-address']['longitude'];
        $coords = "$lat,$lon";
 		error_log("coords: $coords");
        update_post_meta( $post_ID, 'business-location', $coords );
    }
}

The goal is to embed the coordinates from their custom field in a sunset time displayed on the frontend:

<div id="sunsettime"><p style="line-height: 14px; font-size: 12px; margin: 0; padding: 0px 5px 0px 0px;">Today's sunset in</br> [types field='business-city'][/types]</p> <img src="<em><u>hidden link</u></em>" width="30" height="49" alt="" class="wp-image-100 alignnone size-full" /><p style="font-size: 24px; padding: 0px 0px 0px 10px;">

<span id="Birmingham__United_States_z123" style="font-size:24px"></span>
<script src="//widget.time.is/en.js"></script>
<script>
time_is_widget.init({Birmingham__United_States_z123:{template:"SUN", sun_format:"sr12hour:srminute srAMPM", coords:"[types field='business-location'][/types]"}});
</script>
</p></div>

I know that the above code works when I manually enter coordinates into the custom field.

Example with hard coded coordinates: hidden link
Example with code above running: hidden link

#2323719

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I've adjusted the code you have added to the "Custom Code" section as given under:
=> hidden link

add_action( 'save_post', 'ts_save_coordinates',100,3);
function ts_save_coordinates( $post_ID, $post, $update){
	if ( $post->post_type == 'after-death-service' ) {
   	if ( isset($_REQUEST['toolset-extended-form-wpcf']['business-address']) ){
      	error_log("condition met");
        $lat = $_REQUEST['toolset-extended-form-wpcf']['business-address']['latitude'];
        $lon = $_REQUEST['toolset-extended-form-wpcf']['business-address']['longitude'];
     
        $coords = "$lat,$lon";
      
 		error_log("coords: $coords");
        update_post_meta( $post_ID, 'wpcf-business-location', $coords );
    }
    }

Can you please confirm it works as expected now. I can see it does display the time:
=> hidden link

#2324267

YES!!!! Thank you Minesh that fixed my problem. My issue is resolved now. Thank you!