I’m creating a shortcode that outputs the javascript code to create a customized Google Map, like this:
[map w="600" h="400" style="full" z="16" marker="yes" infowindow="<h2>Title</h2>" address="New York"]
Here are some extracts of the code:
function gmap($atts) {
$atts = shortcode_atts(array(
[...]
'infowindow' => '',
[...]
'style' => ''
), $atts);
[...]
//infowindow
if($atts['infowindow'] != '')
{
$thiscontent = htmlspecialchars_decode($atts['infowindow']);
$returncode .= '
var contentString = '' . $thiscontent . '';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
';
}
[...]
return $returncode;
}
Everything is fine if in “infowindow” I only use text, but if I use markup, like <h2>Title</h2>
what I get in the code is:
var contentString = '<br />
<h2>Title</h2>
<p>';
… with two newlines that mess up the js.
Can anybody help me?
Thanks a lot!
Looks like you’re getting screwed over by WordPress’s automatic paragraph formatting.
Option 1: Sometimes you can get around this by switching to the html tab of the editor, then removing any whitespace in your markup, but this also has a tendency to fall apart next time you edit the same page/post.
Option 2: Disable wpautop in your theme’s functions.php:
However, this might screw up your content elsewhere on your site.
Option 3. Use yet another plugin, to toggle wpautop on or off of specific pages: https://wordpress.org/plugins/toggle-wpautop/
Option 4. Add some more shortcode attributes, then apply the required html via PHP, so you don’t have to deal with this!