Saltar navegación

[Resuelto] Copy lat/lng to customfields

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

I need a way to copy/ save the lat lng to two new custom fields (wpcf-latitude & wpcf-longitude) when a user save or update his listing.

Solution:

It requires custom codes, for example:

https://toolset.com/de/forums/topic/copy-lat-lng-to-customfields/#post-1204316

Relevant Documentation:

https://developer.wordpress.org/reference/hooks/save_post/

This support ticket is created hace 5 años, 11 meses. 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
- 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/Hong_Kong (GMT+08:00)

Este tema contiene 4 respuestas, tiene 2 mensajes.

Última actualización por silviaF-3 hace 5 años, 11 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#1203628
toolset_lat_lng.png

Dear support,

i need a way to copy/ save the lat lng to two new custom fields (wpcf-latitude & wpcf-longitude) when a user save or update his listing.
I´ve tried several ways in functions.php from the forum but nothing really works 🙁

F.e. this:
https://toolset.com/forums/topic/latlong-coordinates/ but i think this thread is to old
https://toolset.com/documentation/customizing-sites-using-php/functions/#address

can someone help?

Ps: My php expirence is very low and i have attached a screenshot

Thanks in advance

#1204205

Hello,

Yes, you are right, that ticket is outdated, in the latest version of Toolset Maps plugin, you can get the latitude and longitude values with Types shortcode, for example:

latitude: [types field='address' format='FIELD_LATITUDE'][/types]
longitude [types field='address' format='FIELD_LONGITUDE'][/types]
More help:
https://toolset.com/documentation/customizing-sites-using-php/functions/#address
format:
Use the literals FIELD_ADDRESS, FIELD_LATITUDE and FIELD_LONGITUDE in the format to output the address

So I don't think you need to "copy/ save the lat lng to two new custom fields (wpcf-latitude & wpcf-longitude)".
Is there any special reason for it?

#1204255

Hi,
thank you for your reply.
I need the lat, lng saved in the metatable to use it with another plugin.

Is there an easy way to get the values via function.php and save it in the two fields (wpcf-latiude & wpcf-longitude)?

kind regards

#1204316

There isn't such a built-in feature, and it requires custom codes, for example, there is a custom address field with slug "address", you can try below codes:

function geocode_address($post_id)
{
	remove_action('save_post', 'geocode_address', 999);
    $address = types_render_field('address', array());
    if($address){
		$coordinates = Toolset_Addon_Maps_Common::get_coordinates( $address );
		update_post_meta($post_id, "wpcf-latitude", $coordinates['lat']);
		update_post_meta($post_id, "wpcf-longitude", $coordinates['lon']);
    }
}
add_action('save_post', 'geocode_address', 999);

After you save/update the post, you need to refresh the page, then you will be able to see the latitude and longitude values in your custom fields.

More help:
https://developer.wordpress.org/reference/hooks/save_post/

#1204432

My issue is resolved now. Thank you!