Apr
02
2010

Force WordPress to use the Latest Version of jQuery

by   |  Posted in Tutorials  |  10 comments

If you’re using WordPress 2.9.x, then the latest version of jQuery included is 1.3.2. What if you want to take advantage of new features in version 1.4, like delaying an animation queue or binding multiple event handlers? You can easily add a link in your header to the latest version, but that might end up conflicting with some plugins or themes.

The best approach would be to use the following piece of code to let WordPress know that you want to load the current version instead.

Place this into your theme’s functions.php file:

function current_jquery($version) {
        global $wp_scripts;
        if ( ( version_compare($version, $wp_scripts -> registered[jquery] -> ver) == 1 ) && !is_admin() ) {
                wp_deregister_script('jquery'); 
 
                wp_register_script('jquery',
                        'http://ajax.googleapis.com/ajax/libs/jquery/'.$version.'/jquery.min.js',
                        false, $version);
        }
}
 
add_action( 'wp_head', current_jquery( '1.4.2' ) ); // change number to latest version

If jQuery is updated, all you have to do is change the version number when calling the function.

Reference: Binary Bonsai

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , , , , , , , , ,

Short URL: http://bit.ly/ar61kI

Discussion 10 Comments

  1. Jason on April 5, 2010 at 12:16 am

    That’s some strong kung-fu… Love it! I hate including my own version of jquery in my themes and hardcoding their path in. Now I can do it the right way =)

  2. Gilbert on April 5, 2010 at 1:37 am

    Thanks for modifying the code from Binary Bonsai as it didn’t work on my template.

    However, I’ve found that if you specify an earlier jQuery version in the function (eg 1.2.6), then WordPress still loads the earlier version instead of the more recent bundled version.

    Does this mean the version check doesn’t work?

    • c.bavota on April 5, 2010 at 11:03 am

      For some reason, $wp_scripts stops working when you call it in this instance, so the version check always equals 1. I have done some testing and I can’t for the life of my figure out why all of a sudden $wp_scripts no longer contains any values.

      The script still works, but it will always load whichever version you state instead of actually checking which version is already installed.

  3. giovanna on April 6, 2010 at 2:38 pm

    if you use wp_enqueue_script rather than wp_register_script work :-)

  4. Gilbert on April 8, 2010 at 6:53 am

    Thanks, c. It’s still a useful function nonetheless.

  5. covalic on April 19, 2010 at 9:13 pm

    Great post. I use yours method. Thx…

  6. jfvm on April 28, 2010 at 9:46 pm

    Great, it seems to be exactly what I need but I don’t know where should I paste that code?.
    I’m a newbie in wordpress. Thx

    • c.bavota on April 29, 2010 at 9:07 am

      The only place you should be messing around is in your theme’s directory. Look to see if you have a function.php file in there. If not, create it and then paste the code above into it. Then use the wp_enqueu_script() function in the header.php file.

  7. trusktr on February 23, 2011 at 6:25 pm

    You should rename this post to “Force Your WordPress Theme to Use the Latest Version of jQuery”. :)

  8. bcp software on May 23, 2011 at 3:33 am

    Yes i am using wordpress 2.9.x and was searching for the best tutorial for adding updated version of JQUERY since wordpress 2.9.x contains JQUERY 1.3.2 version but I felt disappointed since I didn’t found any good tutorial at last your tutorial had satisfied me since it worked for me well. I hope this code will really help many people since everyone looks for updated version.