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.

I have built a Wordpress plugin that is based on jQuery and I am using wp_enqueue_script('jquery'); to make it all work.

Let say the default version of jQuery loaded by Wordpress is 1.7.2, what would happen if the users installs a theme based on a different version of jQuery?

  1. What version of jQuery will run in the page where the plugin is called: the default version of jQuery (as I'm using wp_enqueue_script('jquery')) or the version of jQuery called in theme?

  2. I can't figure out what version of jQuery will be used if a page includes multiple plugins and if each one of theme uses a different of jQuery? Will everything mix up and end up in conflicts?

Thanks for your help

share|improve this question
    
Not your problem. If a user installs a plugin/theme that breaks things, it's up to them to sort it out with the developer of that plugin/theme. You should stick to doing what you're doing and if someone complains that their shitty plugin breaks yours by _doing_it_wrong(), tell them to fix their shitty plugin. –  webaware Feb 11 '13 at 22:34
    
OK but still, how does wordpress multiple jquery calls through wp_enqueue_script? If a theme uses jQuery 1.6 and my plugin uses the standard wp_enqueue_script, is it the jQuery version called by the theme that will overwrite the default version I call? thanks –  Vincent Feb 11 '13 at 23:32
    
When a plugin/theme loads another version of jQuery, it either does it by just writing a <script src=... element that loads it, or by forcibly deregistering jQuery (using wp_deregister_script()) and registering their own version. Edit: also by registering/enqueuing with a handle other than 'jquery'. –  webaware Feb 12 '13 at 0:05
add comment

3 Answers

up vote 0 down vote accepted

As other have mentioned , there is no great answer to this, ultimately you cannot control how other people write plugins/themes and there are no standards for naming wp_enqueue_script's, though there probably should be.

Also there is no current way to check if jQuery is loaded using wp_enqueue_script, though this is very easy to do at the template level with something like window.jQuery.

Some notes, since this gets bit weird:

Scenario 1 - Same name

If you use the name jquery to enqueue your script, for example:

wp_enqueue_script( 'jquery', '/ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');

It will not enqueue ^ this link , but instead use the default WordPress bundled jQuery which is:

wp-includes/js/jquery/jquery.js?ver=x.x.x (latest bundled)

The reason is your using the same name, a name registered by WordPress first.

Scenario 2 - Diff name

If you use a different name, for example:

wp_enqueue_script( 'jquery-hi','/ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');

It will just enqueue jquery-hi and not the bundled WordPress jQuery.

Scenario 3 -Multiple names

If you have several plugins/themes using different names, such as:

wp_enqueue_script( 'jquery-hi','/ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
wp_enqueue_script( 'jquery-pie','/ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');

It will enqueue both of them.

ps. Don't load your own jQuery (or other bundled wp script) in the admin, ever.

share|improve this answer
    
Thanks for your very clear and detailed answer. What would be your recommendation in my case? Just stick with default wp_enqueue_script('jquery') and hope for the best? –  Vincent Feb 12 '13 at 12:40
    
yes, I would use jquery , a lot of users deregister the default one and load it via CDN, also jQuery has good backwards compatibility. –  Wyck Feb 12 '13 at 12:44
add comment

Typically the theme developer would declare all the dependencies and include them in the theme. WordPress ships with the latest version of jQuery, but you can always check the exact version in /wp-includes/js/jquery

Hope that helps!

share|improve this answer
add comment

Don't think there is a good answer to your question. This is a kind of problem common when a sort of dynamic linking is being used and I never heard of a good general solution to it. as @webaware said in his comment, in the end it is the user's responsibility to resolve this type of collisions.

Best practice is to depend on the JS supplied with core and test your plugin when new WordPress version comes out to make sure it works with the new versions of the included libraries.

share|improve this answer
add comment

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.