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) 
                '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("#googlemap' . $id . '").gMap({
                                                address: "' . $address . '",
                                                zoom: ' . $zoom . '

        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.

  • I'm not going to add this as an answer because I'm not confident enough...but doesn't your class need to be instantiated? Perhaps outside of your class something like this $var = new map_class(); add_shortcode( 'map', array( $var, 'map_callback' ); Also, if you add your js using wp_enqueue_script() in which case you can specify that it depends on jQuery or whatever and not worry too much about the load order.
    – Will
    Jan 7, 2014 at 4:38
  • @WilltheWebMechanic I tried using $var = new map_class(), which outputs exactly the same as I already have. I don't really know how to use wp_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.
    – tomyam
    Jan 7, 2014 at 4:53
  • oh, wait... First argument is expected to be.... is your theme even using wp_footer() ?
    – Will
    Jan 7, 2014 at 5:04

1 Answer 1


each time you call the shortcode function, you overwrite the previous script var with the current version. make it an array instead of a string:

self::$var[] = $script;

and then output with foreach:

public static function footer() 
    foreach( self::$var as $script ){
        echo $script;
  • Works like a charme. I tried that before with $script[]without success. Thanks a lot.
    – tomyam
    Jan 7, 2014 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.