I am trying to build a Google Maps shortcode using the gMap plugin from Github, not a plain iframe. For which I have to echo a custom inline script for EACH shortcode, right before the closing body tag, after jQuery and all other scripts have been loaded.
The following shortcode works, but it only adds the inline script once, not for every shortcode. When I add three [map] shortcodes, it should add the inline script for each og the shortcodes, three inline scripts, to the footer. But it only adds one script. Which is the one from the last shortcode.
class map_class
{
protected static $var = '';
public static function map_callback($atts)
{
extract(shortcode_atts(array(
'address' => 'Main Street, New York City, United States',
'zoom' => '14'
), $atts));
$id= rand();
$script = '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>';
$script .= '<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#googlemap' . $id . '").gMap({
address: "' . $address . '",
zoom: ' . $zoom . '
});
});
</script>';
self::$var = $script;
add_action( 'wp_footer', array ( __CLASS__, 'footer' ), 20 );
return '<div id="googlemap' . $id . '" class="googlemap" style="height: ' . $height . '"></div>';
}
public static function footer()
{
echo self::$var;
}
}
add_shortcode( 'map', array ( 'map_class', 'map_callback' ) );
$script is holding all three inline scripts, but when I try to call $script in add_action like this:
add_action( 'wp_footer', array ( __CLASS__, $script ), 20 );
.. I get the following error:
"First argument is expected to be a valid callback"
I appreciate any help here.
$var = new map_class(); add_shortcode( 'map', array( $var, 'map_callback' );
Also, if you add your js usingwp_enqueue_script()
in which case you can specify that it depends on jQuery or whatever and not worry too much about the load order.$var = new map_class()
, which outputs exactly the same as I already have. I don't really know how to usewp_enqueue_script()
here, as I don't have a static .js file to load. But that part is fine anyway. I struggle adding the inline script for each shortcode, not only one.wp_footer()
?