Skip to content

Instantly share code, notes, and snippets.

@svenl77
Last active January 11, 2019 17:14
Show Gist options
  • Save svenl77/7e684cd3d4171d0cc009f11b63e96ed8 to your computer and use it in GitHub Desktop.
Save svenl77/7e684cd3d4171d0cc009f11b63e96ed8 to your computer and use it in GitHub Desktop.
<?php
/**
* Enqueue GMW scripts outside the shortcode during page load.
*
* @return [type] [description]
*/
function gmw_custom_load_sctips() {
GMW_Maps_API::load_scripts();
}
add_action( 'wp_footer', 'gmw_custom_load_sctips' );
/**
* Generate and render the map during AJAX request.
*
* @param [type] $map_args [description]
* @param [type] $gmw [description]
*
* @return [type] [description]
*/
function gmw_custom_load_map_via_ajax( $map_args, $gmw ) {
if ( 3 !== absint( $gmw['ID'] ) ) {
return $map_args;
}
// Prepare it for JS.
$map_args = wp_json_encode( $map_args );
?>
<script>
jQuery(window).ready(function () {
var mapArgs = <?php echo $map_args; ?>;
var mapId = <?php echo $gmw['ID']; ?>;
// generate map if not exists
if (typeof GMW_Maps[mapId] == 'undefined') {
// generate map object when ajax is triggered.
GMW_Maps[mapId] = new GMW_Map(mapArgs.settings, mapArgs.map_options, mapArgs.form);
// render the map.
GMW_Maps[mapId].render(mapArgs.locations, mapArgs.user_location);
// Otherwise, update an existing map.
} else {
GMW_Maps[mapId].update(mapArgs.locations, mapArgs.user_location);
}
});
</script>
<?php
return $map_args;
}
add_filter( 'gmw_map_element', 'gmw_custom_load_map_via_ajax', 50, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment