Hi,
I have custom user fields for street, zip code and city.
I want to show these in the posts. That's not a problem.
I also want to use this address-fields for the marker in a map-block. But there is no option to use the authorfields.
How can I use the author's user-fields for an marker in the map-block?
Hello and thank you for contacting the Toolset support.
Map markers can only be pulled from one of the above:
- A Toolset address field.
- Coordinates(latitude and longitude values)
I assume that the existing fields are single-line or number fields, if that's the case, they can't be added to the map. If you have address field or coordinates values, we can work together to build markers for them on the map.
Hi,
I could transfer the data from the individual author-fields to an address-field.
With which hook can fields be processed when the post is opened in the frontend (similar to "cred_save_data")?
Well, there is currently no hook that could let you migrate a single-line address into an address field. Toolset stores address fields in two locations. In the postmeta table, as a string, similar to other Toolset custom fields. And also in a geolocation cache table where the address is stored in a binary way as coordinates. This table is useful for:
- Distance searches.
- As a cache, so Toolset would not need to query Google Maps or Azure Maps APIs every time the address should put as a marker on a map.
I could imagine the following scenario if you have only one field that has the full address as a string:
1. Delete the existing field from Toolset custom fields definitions.
2. Make sure it is completely deleted.
3. Create an address field with the same slug.
4. Create a view of users, with a MAP and put all the results on it.
5. Visit the view in the frontend several times, to force Toolset to query the used Maps API to get these addresses coordinates. Toolset will perform at most 10 queries per minute.
Can you provide more details about the current state of your website and I'll suggest the best way to migrate this into an address field?
- How many users would you like to migrate?
- I understood that you have currently 3 fields(street, zip code, city), right? What are the types of fields? Single lines?
- How are you entering these details? From a frontend form? From the backend?
I already have some code that works.
The street, zip code and city fields are entered individually in the front-end form. The address field is then created from this in the backend.
<?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.
add_action('cred_save_data','func_update_address_custom_field_value',10,2);
function func_update_address_custom_field_value($post_id,$form_data) {
// if a specific form
$forms = array( 1611, 1642 );
if ( in_array( $form_data['id'], $forms) )
{
$street = get_post_meta($post_id, 'wpcf-mitglied_strasse', true);
$zip = get_post_meta($post_id, 'wpcf-mitglied_plz', true);
$city= get_post_meta($post_id, 'wpcf-mitglied_ort', true);
$address = $street." ".$zip." ".$city ;
update_post_meta($post_id, 'wpcf-mitglied_maps', $address);
}
}
I don't want to use the address saved in the post, but that of the author for maps.
In the front end, I could query the individual address data with the following code and combine them into an address field.
$street = [wpv-post-author format="meta" meta="wpcf-benutzer_strasse"]
$zip = [wpv-post-author format="meta" meta="wpcf-benutzer_plz"]
$city = [wpv-post-author format="meta" meta="wpcf-benutzer_ort"]
$address = $street." ".$zip." ".$city ;
But how can I integrate this code so that it works in the frontend?
The markers can be added to a map using the wpv-map-marker shortcode. It just needs to have the ID of the map in the map_id argument.
https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#wpv-map-marker
You just need to pull the data from an address field.
Let's say that you have a similar code(to the first one) that build the address and save it in a user field called (user-address). Then you can use a combination of wpv-map-marker and wpv-post-author shortcodes.
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#vf-154504
Let's assume that the map has an ID "map_5", the shortcode for the marker would be:
[wpv-map-marker map_id="map_5" address="[wpv-post-author format='meta' meta='wpcf-user-address']"][/wpv-map-marker]
Or using the existing fields:
[wpv-map-marker map_id="map_5" address="[wpv-post-author format='meta' meta='wpcf-benutzer_strasse'] [wpv-post-author format='meta' meta='wpcf-benutzer_plz'] [wpv-post-author format='meta' meta='wpcf-benutzer_ort']"][/wpv-map-marker]
Keep in mind, that if you don't use an address field, Toolset will query the Google/Azure Maps API each time a map is viewed, with a limit of 10 requests/min. Because only address fields are cached in the database. This may generate additional fees to the Maps API.
If this does not help, please allow me temporary access and let me check this further. Provide more details, such as where the fields are defined? What view are you working on?
Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
OK.
So that I don't have any problems with the MAP API, I now want to calculate the address field as an additional meta field for the user data and save it.
But my previous code does not work. I suspect it is due to the parameter "$ post_id"
function func_update_user_address_custom_field_value ($ post_id, $ form_data)
since it is not a post.
Which parameters do I have to pass here?
Hi,
I want to remind you of my last question.
My apologies for the late reply. But I do not work on Wednesdays and Thursdays, as you can see on my profile page.https://toolset.com/forums/users/jamal-b/
You are talking about the code in this reply https://toolset.com/forums/topic/using-author-fields-in-a-map-block/#post-1987931 right?
If yes, the function is hooked to cred_save_data, so it will take $post_id and $form_data as arguments.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Maybe the issue, is that you have space between $ and post_id. It should be "$post_id" instead of "$ post_id". But, that should generate an error.
Anyway, the custom code is hooked to a form. Are you using a form to update this data?
The code uses get_post_meta and get_post_meta, which are related to posts. Are the fields saved in a user custom field or in a post custom field?
Hi,
in my code is no space.
It has probably been accidentally pasted into them by copy and paste.
But that's not my question either.
In the code that I have been using so far, I am changing a contribution form. Therefore "$ post_id"
But now I want to change the user data. I probably have to be the user ID. But how exactly do I have to capture that? I tried "$ user_id". That did not work.
Does the code even work with user forms?
Hi,
I found the problem.
I used "update_post_meta" instead of "update_user_meta".