Skip Navigation

[Resolved] Using Javascript to save back latitude and longitude

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by ianH-12 1 year, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2608825

Tell us what you are trying to do?
Save back latitude and longitude based on address field using Javascript

Is there any documentation that you are following?
https://toolset.com/forums/topic/latitude-longitude-fields-2/

Using this code:

jQuery(".editor-post-publish-button").click(function() {
var cords = jQuery("input[name='wpcf[market-location]']").attr("data-coordinates");
var splitcords = cords.split(',');
jQuery("input[name='wpcf[latitude]']").val(splitcords[0].replace("{",""));
jQuery("input[name='wpcf[longitude]']").val(splitcords[1].replace("}",""));

});

The above does not populate the two fields.

#2608937

Hi,

Thank you for contacting us and I'd be happy to assist.

Is there any specific reason, that you'd like to store the address type custom field's latitude and longitude, in separate custom fields?

Using the 'format' attribute, you can extract the latitude and longitude from a Toolset address type custom field:
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/#address )

For example:


[types field='field-slug' format='FIELD_LATITUDE, FIELD_LONGITUDE'][/types]

I hope this helps and please let me know if you have some different requirements.

regards,
Waqar

#2609179

Hi

Thanks for that.

I could still do with them saving back, as we want to use an elementor map block (rather than the Toolset Map) that needs this data as separate custom fields.

#2609275

Thanks for writing back.

On my test website, I was able to achieve this, using a custom function attached to the 'wp_insert_post' hook.
( ref: https://developer.wordpress.org/reference/hooks/wp_insert_post/ )

This approach is much more reliable compared to the script method because it will keep the latitude and longitude values in separate custom fields in sync, no matter how a post is created or updated.

1. For the custom post type 'Books' (slug: book ), I added these custom fields:

a). Address type custom field with slug 'book-address'.
b). Single line type custom field with slug 'book-latitude'.
c). Single line type custom field with slug 'book-longitude'.

2. Next, I added the following custom function, which copies the lat/lon values from the address type custom field to the separate single line type custom fields, every time a 'Book' post is created or updated:


function custom_update_address_fields($post_id, $post, $update) {

	// skip if post revision
	if ( wp_is_post_revision( $post_id ) ) {
		return;
	}

	// skip if post type is not the target post type 'book'
	if ( get_post_type( $post_id ) != 'book' ) {
		return;
	}
	
	// get the lat/lon values from the address type custom field 'book-address'
	$address_field_val = types_render_field("book-address", array( 'item' => $post_id, 'format' => 'FIELD_LATITUDE,FIELD_LONGITUDE' ));

	// If the address field value is not empty
	if(!empty($address_field_val)) {
		// Explode the lat and lon values
		$address_field_val_arr = explode(',', $address_field_val);
		// Update the lat and lon values in the separate custom fields
		update_post_meta( $post_id, 'wpcf-book-latitude', $address_field_val_arr[0] );
		update_post_meta( $post_id, 'wpcf-book-longitude', $address_field_val_arr[1] );
	}

}
add_action( 'wp_insert_post', 'custom_update_address_fields', 9999, 3 );

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Please adjust the custom post type and custom field slugs in the code, as used on your website.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2609519

My issue is resolved now. Thank you! Great solution.