Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active September 17, 2015 16:16
Show Gist options
  • Save danielpataki/2dccea47c8e12f9f5691 to your computer and use it in GitHub Desktop.
Save danielpataki/2dccea47c8e12f9f5691 to your computer and use it in GitHub Desktop.
Google Maps Shortcode
add_shortcode( 'map', 'mgms_map' );
function mgms_map( $args ) {
$id = substr( sha1( "Google Map" . time() ), rand( 2, 10 ), rand( 5, 8 ) );
return "<div class='map' id='map-$id'></div>";
}
add_shortcode( 'map', 'mgms_map' );
function mgms_map( $args ) {
$id = substr( sha1( "Google Map" . time() ), rand( 2, 10 ), rand( 5, 8 ) );
ob_start();
?>
<div class='map' style='height:300px; margin-bottom: 1.6842em' id='map-<?php echo $id ?>'></div>
<script type='text/javascript'>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map-<?php echo $id ?>'), {
center: {lat: 44.6000, lng: -110.5000},
zoom: 8
});
}
</script>
<?php
$output = ob_get_clean();
return $output;
}
<?php
/**
* Plugin Name: My Google Maps Shortcode
* Plugin URI: http://danielpataki.com
* Description: Allows users to add flexible Google Maps to post content
* Version: 1.0.0
* Author: Daniel Pataki
* Author URI: http://danielpataki.com
* License: GPL2
*/
add_action( 'admin_enqueue_scripts', 'mgms_enqueue_assets' );
function mgms_enqueue_assets() {
wp_enqueue_script(
'google-maps',
'//maps.googleapis.com/maps/api/js?key=AIzaSyAzXoaC9OV09c-sTdIWWR1hWzUcJppx_g8&callback=initMap',
array(),
'1.0',
true
);
}
add_shortcode( 'map', 'mgms_map' );
function mgms_map( $args ) {
$args = shortcode_atts( array(
'lat' => '44.6000',
'lng' => '-110.5000',
'zoom' => '8',
'height' => '300px'
), $args, 'map' );
$id = substr( sha1( "Google Map" . time() ), rand( 2, 10 ), rand( 5, 8 ) );
ob_start();
?>
<div class='map' style='height:<?php echo $args['height'] ?>; margin-bottom: 1.6842em' id='map-<?php echo $id ?>'></div>
<script type='text/javascript'>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map-<?php echo $id ?>'), {
center: {lat: <?php echo $args['lat'] ?>, lng: <?php echo $args['lng'] ?>},
zoom: <?php echo $args['zoom'] ?>
});
}
</script>
<?php
$output = ob_get_clean();
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment