Plugin jQuery version VS Theme jQuery version: search for best practice

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

Solutions Collecting From Web of "Plugin jQuery version VS Theme jQuery version: search for best practice"

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 1Same 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 2Diff 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 3Multiple 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.

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!

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.