Skip Navigation

[Resolved] Extracting Latitude and Longitude to a custom field

This support ticket is created 2 years, 11 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 7 replies, has 2 voices.

Last updated by MarkJames 2 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#2298767

I am building a directory site and need the latitude and longitude in a seperate field.

I have read Waqar's helpful reply at https://toolset.com/forums/topic/extracting-longitude-latitude-from-maps-field/#post-2257137 and I am trying to reproduce this myself.

The custom post type is PA Profile.
The map field is wpcf-street-address
The field I want to put the co-ordinates into is wpcf-location

My code is as follows

// add long/lat
add_action( 'save_post', 'custom_process_longlat_save_post', 10,3 );
function custom_process_longlat_save_post( $post_id, $post, $update ) {
    if ( ('pa-profile' !== $post->post_type) OR ($post->post_status == 'auto-draft') OR (empty($_POST)) ) {
        return;
    }
     
    $post_id = $post->ID;
    if(!empty($_POST['wpcf-street-address'])) {
        // removed line: $street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
        $street_location = $_POST['toolset-extended-form-wpcf-street-address']['latitude'].', '.$_POST['toolset-extended-form-wpcf-street-address']['longitude'];
        custom_process_new_longlat($post_id, $street_location);
    }
      
    if(!empty($_POST['wpcf']['street-address'])) {
        // removed line: $street_location = do_shortcode( '[types field="street-address" format="FIELD_LATITUDE, FIELD_LONGITUDE"][/types]' );
        // removed line: custom_process_new_longlat($post_id, $street_location);
        $street_location = $_POST['toolset-extended-form-wpcf']['street-address']['latitude'].', '.$_POST['toolset-extended-form-wpcf']['street-address']['longitude'];
        $_POST['wpcf']['street-address'] = $street_location;
    }
}
   
function custom_process_new_longlat($post_id, $street_location) {
        update_post_meta( $post_id, 'wpcf-location', $street_location );
}

However, it just doesn't work.

Can anyone see why this doesn't work.

Many thanks

Mark

#2299065

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Mark

Before helping with the specifics, can we just confirm, this code is intended to work on the back-end post-edit screens, is that where you are expecting to use it?

Also, you say you want to store the latitude and longitude in a custom field wpcf-location. In what format? Just as a comma-separated list? JSON-format? A serialised array?

#2299801

Hi Nigel

The code is meant to be used back end to popular the field wpcf-location with a single value ie 50.82233,-0.447337047.

This is so the co-ordinates can be used for a different purpose.

I am aware of how toolset currently store this data, for caching purposes.

Many thanks

Mark

#2299855

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, so I think this code is all you need, it's simpler than the example above:

add_action( 'save_post_pa-profile', 'ts_save_coordinates' );
function ts_save_coordinates( $post_ID ){

    if ( isset($_POST['toolset-extended-form-wpcf']['address']) )
    {
        $lat = $_POST['toolset-extended-form-wpcf']['address']['latitude'];
        $lon = $_POST['toolset-extended-form-wpcf']['address']['longitude'];
        $coords = "$lat,$lon";

        update_post_meta( $post_ID, 'wpcf-location', $coords );
    }
}
#2300223

Thanks for the code, unfortunately it didn't work.

Do you know of a way I can debug so I can see if the variables are being created e.g. $coords

Many thanks

Mark

#2300707

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Mark

I did test the code before sharing it, it ought to work.

It has hard-coded the slug of the post type ("pa-profile") and the custom field you want to create ("location", which with Types will have a wpcf- prefix, so "wpcf-location").

Assuming those are correct, to debug be sure to turn on your debug log.

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DISABLE_FATAL_ERROR_HANDLER',true);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor.

Update your code to sprinkle some error_log calls which will send messages to the debug.log, e.g.

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

If you are not sure about how to interpret what you find, let me know...

#2301049

Thank you for taking the time to show me how to debug code.

This helped me understand what was going on and debug it.

I ended up with the following code

add_action( 'save_post_pa-profile', 'ts_save_coordinates' );
function ts_save_coordinates( $post_ID ){
  
    if ( isset($_POST['toolset-extended-form-wpcf']['street-address']) )
    {
       
        $lat = $_POST['toolset-extended-form-wpcf']['street-address']['latitude'];
        $lon = $_POST['toolset-extended-form-wpcf']['street-address']['longitude'];
        $coords = "$lat,$lon";
        
        update_post_meta( $post_ID, 'location', $coords );
    }
}

I couldn't get the co-ordinates to appear in the toolset single line text file wpcf-location nor another number field I created, but I can store it in an ordinary meta field - location.

This is fine for my use.

Thanks again.

Mark

#2301969

My issue is resolved now. Thank you!