I’m developing a plugin that use a Google Maps API. The plugin enqueuing script in this way:
wp_enqueue_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false&callback=initialize&language=en-us', array('jquery'), false, true);
Considering the fact that other plugins/themes may use same maps library, while $handle
could be different, the validation wp_script_is($handle,'registered')
does not makes sence. Duplicating of enqueued script leads to JS error:
You have included the Google Maps API multiple times on this page. This may cause unexpected errors.
Regarding things described I’ve build a code that search thought $wp_scripts
for Google Maps scripts and unset it, in case if found:
global $wp_scripts;
foreach ($wp_scripts->registered as $key => $script) {
if (preg_match('#maps\.google(?:\w+)?\.com/maps/api/js#', $script->src)) {
unset($wp_scripts->registered[$key]);
}
}
The question is: how can I check and reassign dependencies of the removed scripts (in case if they’re set by other plugins/themes). What will happens and how to deal with the fact that multiple plugins may use the same &callback=initialize
parameter of Google Maps API script.
3 s
EDIT :
use wp_enqueue_script
as you want (enqueue in header or footer after jQuery as you want) to enqueue file called something like : gmap.js
included in your plugin
wp_enqueue_script('custom-gmap', plugin_dir_url( __FILE__ ).'inc/js/gmap.js', array('jquery'), false, true);//just change your path and name of file
write this in your Js file 🙂
$(document).ready(function() {
if (typeof google === 'object' && typeof google.maps === 'object') {
return;
}else{
$.getScript( 'http://maps.google.com/maps/api/js', function() {});
}
});