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.
Hi, nice idea, but for me the map doesn’t show up either, only a blank field. Any suggestions what went wrong???
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.