Skip Navigation

[Resolved] Conditional to hide address if its located by pin and showing lat/long coordinat

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 5 replies, has 2 voices.

Last updated by Waqar 1 year ago.

Assisted by: Waqar.

Author
Posts
#2582803
Captura de Pantalla 2023-03-28 a la(s) 23.01.16.png

Tell us what you are trying to do?

There are some address that get not the right pin on the map, so, when Im selecting the pin myself on the map, the output address changes to something like this {18.39991672136518,-66.15706619049072}

My client doesn't want to see this kind of address on front end, but want to see an address of this type i.e: 52XH+H5 San Lorenzo, Puerto Rico

So the question is: Is that a way to have the correct "name" of the address when selecting it on the map?

If not possible, I think I can try creating another custom field, so when lat/long address appears, making a conditional, I can avoid showing the lat/long address and show this new custom field instead?

I went ahead and trying, but conditional only shows if equal, not equal, less than, larger than etc.. but as address show into brackets {} Im not being able to mark that conditional

Please help

Thanks in advance

#2582913

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

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

When an exact location/pin is selected on a map, without providing any human-readable address or location, its longitude, and latitude are stored as the address custom field's value, inside the curly brackets, as you noted.

If your goal is to show that address type custom field value on the front end, only when a human-readable address is available, you can register a custom shortcode, like this:


add_shortcode('show-custom-address-field', 'show_custom_address_field_func');
function show_custom_address_field_func() {

	$field_output = do_shortcode("[types field='book-address'][/types]");

	if(!(str_contains($field_output, '{'))) {
		return $field_output;
	}
	
}

Note: Please replace 'book-address' with the actual slug of your address type field.

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.

Next, you can use this new shortcode in your template or view to show this custom field value, only when a human-readable address is available:


[show-custom-address-field]

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2583223

Thanks for your fast answer

I did try your code, and what I've noticed is that when a lat/long address is present, the shortcode prevent it to show that.

In my case, I need that all addresses shows as human-readable type.

I did make a research on reverse geocoding API but I really dont know how to use it within Toolset maps.

My second approach is as follows:

Create a custom field that I can call something like "alternative address" that conditionally only be present if the given address is lat/long type
So, if user for example, pin the address on the map, that field will be displayed to give the user the opportunity to manually write the address.

In the template loop, the idea is the same, to show that custom field only, and only if lat/long address type is present, and if that's the case, then not to display the lat/long address.

Do you have an approach for my case?

Thanks in advance

#2583421

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

Introducing a custom reverse geocoding API call can result in a conflict with the address field's default functionality, so it would be better to adopt the second approach.

With the address type field, let's call it 'Listing Address', you can introduce a single line type field 'Listing Address Description'.

In the address type field, the users can select the exact address or the pin location on the map, and in the single line field, they can save short human-readable information/description about that selected location.

And, then you can update the shortcode to output the text from the description field when the address field contains only the lat/long coordinates:


add_shortcode('show-custom-address-field', 'show_custom_address_field_func');
function show_custom_address_field_func() {

	$field_output = do_shortcode("[types field='listing-address'][/types]");
	$desc_field_output = do_shortcode("[types field='listing-address-description'][/types]");

	if(!(str_contains($field_output, '{'))) {
		return $field_output;
	} else {
		return $desc_field_output;
	}
	
}

#2585563

Hey Waqar

Thank you so much. It worked just perfect!

Best

#2585683

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and glad that I was able to help.

You're welcome to mark this ticket as resolved and start a new one for each new question or concern.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.