Skip to content

Instantly share code, notes, and snippets.

@chrisblakley
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisblakley/0bf33b0975ef86053ee1 to your computer and use it in GitHub Desktop.
Save chrisblakley/0bf33b0975ef86053ee1 to your computer and use it in GitHub Desktop.
Allow for embedding Google Maps into your Wordpress posts
add_shortcode('map', 'map_shortcode');
function map_shortcode($atts){
extract( shortcode_atts(array("key" => '', "mode" => 'place', "q" => '', "center" => '', "origin" => '', "destination" => '', "waypoints" => '', "avoid" => '', "zoom" => '', "maptype" => 'roadmap', "language" => '', "region" => '', "width" => '100%', "height" => '250', "class" => '', "style" => ''), $atts) );
if ( $key == '' ) {
$key = ''; //@TODO: Replace with your own key to avoid designating a key every time.
}
if ( $q != '' ) {
$q = str_replace(' ', '+', $q);
$q = '&q=' . $q;
}
if ( $mode == 'directions' ) {
if ( $origin != '' ) {
$origin = str_replace(' ', '+', $origin);
$origin = '&origin=' . $origin;
}
if ( $destination != '' ) {
$destination = str_replace(' ', '+', $destination);
$destination = '&destination=' . $destination;
}
if ( $waypoints != '' ) {
$waypoints = str_replace(' ', '+', $waypoints);
$waypoints = '&waypoints=' . $waypoints;
}
if ( $avoid != '' ) {
$avoid = '&avoid=' . $avoid;
}
}
if ( $center != '' ) {
$center = '&center=' . $center;
}
if ( $language != '' ) {
$language = '&language=' . $language;
}
if ( $region != '' ) {
$region = '&region=' . $region;
}
if ( $zoom != '' ) {
$zoom = '&zoom=' . $zoom;
}
return '<iframe class="nebula-googlemap-shortcode googlemap ' . $class . '" width="' . $width . '" height="' . $height . '" frameborder="0" src="https://www.google.com/maps/embed/v1/' . $mode . '?key=' . $key . $q . $zoom . $center . '&maptype=' . $maptype . $language . $region . '" style="' . $style . '"></iframe>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment