Skip Navigation

[Resolved] How to creade php code for conditional output Google maps

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)

Author
Posts
#2552945

Tell us what you are trying to do? In my page template I am able to add those short codes to show Google maps.

[wpv-map-render map_id="map-1" map_width="100%" map_height="300px"][/wpv-map-render]
[wpv-map-marker map_id='map-1' marker_id='marker-1' marker_field='wpcf-address-google-maps'][/wpv-map-marker]

This is so far working.

But now, I like to have an own short code - where both short codes above are in - basically only showing, when we have the field Map Address filled out.

I am trying to do as following:

function func_display_map($atts) {
global $post;
$str = '';

$field_value = types_render_field("address-google-maps", array());

if(!empty($field_value)){
echo do_shortcode('[wpv-map-render map_id="map-1" map_width="100%" map_height="300px"][/wpv-map-render]' );
}
}
add_shortcode( 'show_villa_map', 'func_display_map' );

But it is not working as expected, it just shows nothing.

Is there any documentation that you are following? Not really found a good documentation

Is there a similar example that we can see? hidden link

What is the link to your site? hidden link

#2553463

Hi,

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

To include the map and the map marker shortcode, only when the address field is not empty, you can directly place it inside the conditional statement:
( ref: https://toolset.com/documentation/legacy-features/views-plugin/using-shortcodes-in-conditions/ )


[wpv-conditional if="( '[types field='address-google-maps'][/types]' ne '' )"] 

[wpv-map-render map_id="map-1" map_width="100%" map_height="300px"][/wpv-map-render]
[wpv-map-marker map_id='map-1' marker_id='marker-1' marker_field='wpcf-address-google-maps'][/wpv-map-marker]

[/wpv-conditional]

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

regards,
Waqar

#2553497

Hello

This isn't working. I tried this already.

And this is why I would like to create some php code. But not sure how I can include short code into php.

Regards,

Simon

#2553587

Hi Simon,

On my test website, the conditional code method (from the last message) works as expected. For the PHP code shortcode, I tested your code with some changes and it also works correctly:


function func_display_map($atts) {
	global $post;

	$field_value = types_render_field("address-google-maps", array('item' => $post->ID));

	if(!empty($field_value)){
		ob_start();
		echo do_shortcode('[wpv-map-render map_id="map-1" map_width="100%" map_height="300px" item="'.$post->ID.'"][/wpv-map-render]' );
		echo do_shortcode('[[wpv-map-marker map_id="map-1" marker_id="marker-1" marker_field="wpcf-address-google-maps" item="'.$post->ID.'"][/wpv-map-marker]' );
		return ob_get_clean();
	}
}
add_shortcode( 'show_villa_map', 'func_display_map' );

In case the issue still persists, you're welcome to share temporary admin login details, along with the link to the page where these shortcodes are not working.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2553609

My issue is resolved now. Thank you!