RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.59%*) • Fixed: 5.39% (6.59%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.48%*) • Investment PI: 5.39% (6.59%*)

Embed Google Maps in WordPress Using Shortcode

This article will provide you with a WordPress function that'll permit you to post Google Maps inside of your WordPress post or page using shortcode. It's ported over from the retired Internoetics, but it still works (despite being a little dated). You're required to register for an API key, and you should register your embed page and/or server IP to ensure your key isn't abused if publicly displayed.

The Result

Using the shortcode of [googlemap src="8 cavendish st concord west nsw 2138"] we'll render the following map (the address is one formerly owned by family).

Available attributes are detailed below. The shortcode should be modified to your preferred defaults so you'll only ever need to specify the src (address).

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Embed Google Maps in WordPress Using Shortcode
4
 http://www.beliefmedia.com/code/wp-snippets/embed-google-maps
5
*/
6
 
7
function beliefmedia_google_maps($atts, $content = null) {
8
  extract(shortcode_atts(array(
9
    'width' => '560',
10
    'height' => '280',
11
    'apikey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
12
    'zoom' => '',
13
    'centre' => '',
14
    'maptype' => 'roadmap', /* roadmap (default) or satellite */
15
    'language' => '',
16
    'region' => '',
17
    'src' => ''
18
  ), $atts));
19
 
20
  $src = str_replace(' ', '+', $src);
21
  $return = '
22
<p align="center"><iframe width="' . $width . '" max-width="90%" height="' . $height . '" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=' . $apikey . '&q=' . $src;
23
  if ($zoom) $return .= '&zoom=' . $zoom;
24
  if ($centre) $return .= '&centre=' . $centre;
25
  if ($maptype) $return .= '&maptype=' . $maptype;
26
  if ($language) $return .= '&language=' . $language;
27
  if ($region) $return .= '&region=' . $region;
28
  $return .= '"></iframe>';
29
 
30
 return $return;
31
}
32
add_shortcode("googlemap", "beliefmedia_google_maps");

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

Shortcode Attributes

Once you've copied the code as required, the only attributes that are really required are width, height and src. Once width and height are set, it'll only be src (address) that's ever required in your shortcode. Other options are not required.

Available attributes are as follows:

width

The width of the Google Map (iframe). It should normally be set to a little less than the width of your post container. By default, it will center.

height

The height of the iframe.

src

The address or location you wish to render on your website. Can include spaces (we'll replace spaces as required when the code is evaluated).

maptype

maptype can be either roadmap (the default) or satellite, and defines the type of map tiles to load.

apikey

Required. Register for a free key here .

zoom

If set, zoom will set the initial zoom level of the map. Accepted values range from 0 (the whole world) to 21 (individual buildings). The upper limit can vary depending on the map data available at the selected location.

center

center defines the center of the map view. It accepts a comma-separated latitude and longitude value (such as 37.4218,-122.0840).

language

language defines the language to use for UI elements and for the display of labels on map tiles. Note that this parameter is only supported for some country tiles; if the specific language requested is not supported for the tile set, then the default language for that tileset will be used. By default, visitors will see a map in their own language.

region

region defines the appropriate borders and labels to display, based on geo-political sensitivities. Accepts a region code specified as a two-character ccTLD (top-level domain) value.

Another Example

Using some of the available attributes, here's another look at the old address (the old house is replaced by dodgy townhouses). Shortcode used is as follows: [googlemap src="8 moala st concord west nsw 2138" zoom="19" maptype="satellite"].

We'll update the code soon.

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment