X

How To Create a Google Maps Shortcode

Snippets by IsItWP

Are you looking for a way to use a shortcode to add Google Maps to your WordPress site? While there’s probably a plugin for this, we have created a quick code snippet that you can use to create a Google Maps shortcode in WordPress.

Instructions:

Add this code to your theme’s functions.php file or in a site-specific plugin.

function fn_googleMaps($atts, $content = null) {
       extract(shortcode_atts(array(
                    "width" => '640',
                    "height" => '480',
                    "src" => ''
                    ), $atts));
      return '<iframe width="'.$width.'" height="'.$height.'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'.$src.'"></iframe>';
}
add_shortcode("googlemap", "fn_googleMaps");

Then, use this shortcode to create a map. Remember to change the src attribute.

[googlemap width="200" height="200" src="[url]"]

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: Google Maps Builder Review: Easily Embed Interactive Maps and how to set up author tracking in WordPress with Google Analytics.

Comments   Leave a Reply

  1. Hi, nice idea, but for me the map doesn’t show up either, only a blank field. Any suggestions what went wrong???

  2. Hi Cliff a few things including if changes are made to the embed code in the embed code you can just update the snippet rather then update each post. You may want to have multiple sizes for each post or you may in the future change the design of your site and need each map a specific size that you could ignore the size att and force a default one.

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!