Updating jQuery in WordPress

This article might not be very relevant for the most of you, but since it kept me busy for quite some time I do want to share it, just in case somebody else is looking for info on this.

WordPress is shipped with a version of jQuery included in it. But since jQuery is updating its functionality at a much higher pace than WordPress is, the current version of jQuery used in WordPress is not very up-to-date anymore. And this might be necessary in some cases.

For those of you who are not familiar with jQuery just yet: jQuery is a script that simplifies several very advanced and complex applications of JavaScript . If you look on the front page of StudioStacks for example you’ll find a Like-button on the left side of you screen and when clicking it, a little window slides into your screen. Because I did this with jQuery, it only took me a few lines of code. It’s really easy and if you’re interested, I highly recommend having a look at their large collection of tutorials. Even with a very basic level of knowledge of JavaScript you will already be able to code amazing effects with jQuery after only a few hours of studying.

By adding the following code to your template’s functions.php your template can use the latest version of jQuery while leaving the shipped version of jQuery intact so your admin panel will remain using its own version (you could seriously mess up its functionality):

function scripts() {
if ( !is_admin() ) { // this makes sure that the admin panel remains using the old version
         // jquery
               wp_deregister_script('jquery'); // turns off WordPress' edition of jQuery
               wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"), false); // the new version of jQuery (in this code it uses the version hosted by Google but of course you can point this to any location and host it yourself)
               wp_enqueue_script('jquery'); // Now it's automatically applied all over your templates, but you can do this on parts as well
add_action( 'wp_print_scripts', 'scripts'); // this executes the script

Hopefully this has been to useful for some of you.


Follow any comments here with the RSS feed for this post, Post a comment or leave a trackback: Trackback URL.



Leave a Reply

Your email address will not be published. Required fields are marked *