Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Download a copy of Modernizr, put it in your plugin’s directory and simply enqueue the script whenever needed. Done.

The problem is that Modernizr may already be loaded by another plugin. You wouldn’t want to load it more than once. How can you find out if Modernizr is already available before enqueueing it yourself? This goes for any other JavaScript library really.

You could check whether a script was loaded by using the wp_script_is() function. However, that function expects the name of the handle the script was registered by. You can only guess, right? Even if we assume the handle name is simply "modernizr", how do you check for a certain version (like 2.6.1)? Or worse, since custom Modernizr builds are possible, how do you check the features you need are included?

The whole wp_enqueue_script() setup works fine for libraries that ship with WordPress by default, like jQuery, jQuery UI, Thickbox, etc. Their handle names are documented and you know which version to expect. WordPress doesn’t provide such master files for all JavaScript libraries, of course.

Am I missing something here or is this just the messy situation you somehow have to deal with when developing plugins?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

wp_enqueue_script() utilizes the WP_Scripts class, which extends the WP_Dependencies class. If you checkout that class, you will see a bunch of arrays at the top. So you could actually loop through list of queued scripts like so:

global $wp_scripts; // If inside a function

foreach($wp_scripts->queue as $script) {
    // Do some tomfoolery here... ;)
}

Also, if you use wp_enqueue_script(), it won't load the script more than one time. It actually checks to see if it's been enqueued, so I think the above is moot anyway. Good to know either way.

References:

http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/functions.wp-scripts.php

http://core.trac.wordpress.org/browser/trunk/wp-includes/class.wp-dependencies.php

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

share|improve this answer

This really shouldn't matter as long as both plugin authors are following standards. I am assuming that you are talking about enqueueing on the admin part. Both plugins should only enqueue on their admin pages so no conflict should arise. Even if it does - it isn't the end of the world as loading 2 copies won't cause a JavaScript error and you can check which version of modernizr is loaded with Modernizr._version.

share|improve this answer
    
I’m not talking about admin pages only. For the plugin I have in mind I’ll need Modernizr on the front-end too. The site probably won’t break if Modernizr gets loaded twice by different plugins; however, I’m looking for best practices here. Imaging your WP installation uses five plugins which all load Modernizr on their own. –  Geert Sep 4 '12 at 18:19
    
Then assuming both plugin authors used wp_enqueue_script and called it Modernizr (as that is its name) then everything should be fine. –  Daniel Chatfield Sep 8 '12 at 15:28

If the plugin is well written is should be using wp_enqueue_script() and thus solves your issue.

If the plugin is poorly written and loads it another way then you will have to use some hacky solutions to match by name, version, header or something else more fancy. There is no way to control how people write plugins especially of they are changing file names.

You can also check if Modernizer is loaded using javscript.

share|improve this answer
    
Still, how do you deal with the unknown handle name? –  Geert Aug 19 '12 at 19:34
    
You typically don't unless you use PHP to scan all .js files for some sort of match, it would be better to just check via JavaScript, which is outside the scope of WordPress. –  Wyck Aug 19 '12 at 19:48
    
Okay, as a note to myself, here’s a way to load JavaScript files without using any external libraries: jspatterns.com/the-ridiculous-case-of-adding-a-script-element –  Geert Sep 4 '12 at 18:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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