Here’s a little hack I worked out to solve a dilemma that didn’t appear to be particularly well documented.
My problem was that I needed to use jQuery 1.3 for some script but the default version in Wordpress 2.7 is v1.2.6. It also makes sense to load the library from a common source such as the Google Ajax Libraries API store which increases the chances of the site visitor already having a cached version of that file in their browser (decreasing the page load cost and consequently the page load time). Simply adding the script tag to the page header works, but any plugins or other WordPress functions which require jQuery will load the default copy of jQuery in addition to the one that you have added.
Get a handle on it
After digging around in the Wordpress Codex and some other helpful blogs I managed to get to grips with the in-built system for loading javascript files.
The way it works is that WordPress registers script libraries against defined ‘handles’. In this example I’m replacing the default registration for the jquery handle with version 1.3.2 loaded from the Google Ajax Libraries API store.
In my Theme Functions script (functions.php) I added these two simple lines of code.
wp_deregister_script( 'jquery' ); wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2' );
wp_deregister_script drops the default script location for the jQuery library and releases the ‘jquery’ handle. wp_register_script assigns a new script location to the jquery handle. this function has no effect if the handle being used is already registered, hence the use of wp_deregister_script beforehand.
Now anything which uses the Jquery library as a dependancy will use the new version (providing it has been coded using the wp_enqueue_script function) e.g.
to load a jquery plugin in a theme template file such as the page header use:
<?php wp_enqueue_script("jqueryplugin", "/wp-content/themes/mytheme/jqueryplugin.js", array("jquery"),"1.0"); ?>
this will result in the page output:
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script> <script type='text/javascript' src='http://mydomain/wp-content/themes/mytheme/jqueryplugin.js?ver=1.0'></script>
This example only replaces the jquery definition, WordPress includes a number of common javascript libraries, many of which could benefit from this treatment.
Warning Note
Replacing the default version of a Javascript library could lead to errors in other scripts which depend on that library – always test thoroughly.
The default WordPress script loads jQuery in compatibility mode, to use jQuery alongside other libraries, you will need to adjust your scripts to enable compatibility.
Tweet This
Digg This







New blog post: Handy WordPress hack – changing javascript library location http://tinyurl.com/dexe4o
This comment was originally posted on Twitter
Handy #WordPress hack – changing javascript library location http://tinyurl.com/dexe4o
This comment was originally posted on Twitter
Handy #WordPress hack – changing javascript library location http://tinyurl.com/dexe4o (via @Techn0tic)
This comment was originally posted on Twitter
Thanks for this. works great.
The use-google-libraries works well and removes the ?ver= from the end of the script.
in order for it to use the 1.3.2 I do what you have mentioned above as well as installing the plugin
Glad this has helped Josh. The script registration stuff is pretty sparsely documented so I thought I’d try and write something helpful. There’s a similar system for including stylesheets which I’m going to tackle sometime soon.
I really very liked this post. Can I copy it to my blog? Thank you in advance. Sincerely
Great post, this is exactly what I needed for development of a WP plugin.
Just what I was looking for: http://tinyurl.com/dexe4o
This comment was originally posted on Twitter
Okay. Wonderful. Fantastic. But perhaps you could do an easy 1. 2. 3. step tutorial, because trying to get jquery loaded on my site has been a ******* nightmare! A couple of hundred googles later and I’m still no closer!