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
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?
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
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/
My issue is resolved now. Thank you!