-
-
Save barbietunnie/2e9b6b98b1ef27d274f9 to your computer and use it in GitHub Desktop.
Google Maps Shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>"; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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