Skip to content

Instantly share code, notes, and snippets.

@camaleaun
Last active May 5, 2017 00:02
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 camaleaun/ea4e3325a7236914cae3d149bbcb05c3 to your computer and use it in GitHub Desktop.
Save camaleaun/ea4e3325a7236914cae3d149bbcb05c3 to your computer and use it in GitHub Desktop.
WP Short Code to Google Maps JS API Embed
<?php
if ( ! class_exists( 'Mapa_Shortcode' ) ) :
/**
* Shortcode [mapa]
*
* Pega atributos através do place="lat,lng"
*
* Ex.: [mapa place="-26.2458151,-49.3858951"]
* Mostra o script e: <div id="map" class="map"></div>
*
* @author Xthor
* @link https://gist.github.com/camaleaun/ea4e3325a7236914cae3d149bbcb05c3
*/
class Mapa_Shortcode {
static $adicionar_script;
static function init() {
add_shortcode( 'mapa', array( __CLASS__, 'trata_shortcode' ) );
add_action( 'init', array( __CLASS__, 'registra_script' ) );
add_action( 'wp_footer', array( __CLASS__, 'insere_script' ) );
}
static function trata_shortcode( $atts ) {
self::$adicionar_script = true;
extract(shortcode_atts(array(
'place' => '',
'width' => '100%',
'height' => '400'
), $atts));
$style = "style=\"width:{$width};height:{$height}px;\"";
ob_start();
?>
<div id="map" class="map"<?php echo $style; ?>>;
<input type="hidden" id="place" value="<?php echo $place; ?>">
</div>
<?php
return ob_get_clean();
}
static function registra_script() {
wp_register_script( 'gmaps-api', "//maps.googleapis.com/maps/api/js", array(), '3.0', true );
wp_register_script( 'mapa', plugins_url('assets/js/mapa.js', XT_PLUGIN_FILE), array(), '1.9.2', true );
}
static function insere_script() {
if ( ! self::$adicionar_script )
return;
wp_print_scripts( 'gmaps-api' );
wp_print_scripts( 'mapa' );
}
}
Mapa_Shortcode::init();
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment