Drupal 7: Manually update jQuery version

Drupal Today for a customer I had to use a special plugin for jQuery that needs a jQuery version equal or major than 1.6.1.

As you know Drupal 7 is shipped with jQuery 1.4.4 and so is not ready for the plugin I need to use, there is a plugin that update jQuery and jQuery UI but where is the fun?

The procedure is pretty quick:

  • Download the jQuery version you need from from the official source.
  • Place the jQuery version you downloaded in your theme js’s folder (like MYTHEME/js/).
  • Create/Modify your theme’s template.php file as described below.

That is.

Now for the template.php, you need to use the hook_js_alter to replace the native jQuery:

function MYTHEME_js_alter(&$javascript) {
  //We define the path of our new jquery core file
  //assuming we are using the minified version 1.8.3
  $jquery_path = drupal_get_path('theme','MYTHEME') . '/js/jquery-1.8.3.min.js';

  //We duplicate the important information from the Drupal one
  $javascript[$jquery_path] = $javascript['misc/jquery.js'];
  //..and we update the information that we care about
  $javascript[$jquery_path]['version'] = '1.8.3';
  $javascript[$jquery_path]['data'] = $jquery_path;

  //Then we remove the Drupal core version
  unset($javascript['misc/jquery.js']);
}

After we clear the cache we will have the new jQuery version.

Keep in mind that changing the jQuery version may create some incompatibility issues with the other Drupal core scripts.

Enjoy!

Advertisements

4 thoughts on “Drupal 7: Manually update jQuery version

  1. jQuery does not seem to be listed in admin/reports/status – is there some way to check what version of jQuery my D7 site is currently using? Thanks…

Your opinion is important!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s