5

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.

4
  • 1
    Maybe localize script could contain a collection of different callbacks, then you only enqueue a script that points to your collection callback which will cycle through all initializers in JS. You just need to parse off the original callbacks in any currently registered scripts before you dequeue them.
    – jgraup
    Commented Mar 1, 2016 at 15:36
  • 1
    what about using your js file included in your plugin then check if var google.maps.Load is exist (or any unique var or class google map used) >> if not exist you can getScript from google .... I don't know if worked but i like your question Commented Mar 2, 2016 at 19:13
  • Good suggestions! I'll try to workaround using this and make a report. With all the respect to @Mark Kaplun's answer i'm not a supporter of "leave as it is" is terms of development.
    – Ignat B.
    Commented Mar 2, 2016 at 19:34
  • Why not create an option for the user to choose whether or not to include the API with your plugin. If they are including it somewhere else then they can just turn yours off.
    – thebigtine
    Commented Mar 6, 2016 at 17:33

3 Answers 3

8
+25

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() {});
    }
});

http://codepen.io/khaled-sadek/pen/WwvOGb

2
  • Thank you for solution provided, but it does not answer in terms of enqueueing the script. For example what if i'll need to put gmaps script in the footer or right after jquery.
    – Ignat B.
    Commented Mar 2, 2016 at 20:22
  • @Ignet B. ok update the answer , the key is use specific js file to this mission and enqueueing it as you like Commented Mar 2, 2016 at 20:33
5

Don't stress about it. There are many ways in which plugins and themes can break each other and you just can not avoid it when everything happens in the same execution space/context.

You should test against whatever plugins and themes you want to make sure your code works with but there is just no way to make everybody at every point in time happy. Actually it is kind of hubris from your side to decide that your enqueue should be given priorities over others....

9
  • Thank you for prompt answer. I understand that it's not fair to block other plugins enqueues, due to that my question where asked: want to enqueue in a smooth way to cooperate with plugins, avodigin errors. Looks like i'd let things go and simply test with top-used plugins.
    – Ignat B.
    Commented Feb 26, 2016 at 20:12
  • @Mark but if can help avoid such conflicts in the first place? finding a way to detect and workaround potential conflicts is better than trying to be compatible with X themes/plugins which is basically futile.
    – majick
    Commented Mar 8, 2016 at 16:26
  • @majick, it is not your problem as a plugin author. As I said it is hubris to assume your code is more important then the code in other plugins. If there is a collision then the site owner will see it when he activates the plugin and will probably deactivate it. If it is such a small problem that the site owner can not even see it, then there is nothing to fix in the first place Commented Mar 8, 2016 at 16:33
  • how does that help? assuming the site owner can see there is a conflict, it is then left to them to deactivate one plugin or the other, or find a way to fix the conflict themselves. this kind of foresight for a plugin author should be encouraged not written off as "not my problem" = this is why conflicts happen in the first place.
    – majick
    Commented Mar 9, 2016 at 8:39
  • @majick, I don't get your point. I have two plugins on my site A and B. When both active the site goes blank. How can anyone which is not the site owner know which plugin is more important to the site? Commented Mar 9, 2016 at 9:18
0

Rather than removing a script if it has already been enqueued, you could combine your answer to check for the Google Maps script and add the fallback to the javascript check suggested by @Kiki...

add_action('wp_enqueue_scripts','google_maps_script_loader',999);

function google_maps_script_loader() {
    global $wp_scripts; $gmapsenqueued = false;
    foreach ($wp_scripts->registered as $key => $script) {
        if (preg_match('#maps\.google(?:\w+)?\.com/maps/api/js#', $script->src)) {
            $gmapsenqueued = true;
        }
    }

    if ($gmapsenqueued) {
        wp_enqueue_script('custom-gmap', plugin_dir_url( __FILE__ ).'inc/js/gmap.js', array('jquery'), false, true);
    } else {
        wp_enqueue_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false&callback=initialize&language=en-us', array('jquery'), false, true);
    }
}
2
  • Good suggestions, but i think this function should be hoocked to run after all plugins & theme enqueueing their script Commented Mar 8, 2016 at 14:30
  • ok I've added the hook to the edit...
    – majick
    Commented Mar 8, 2016 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.