How To Always Use The Latest jQuery Library By Default On Your WordPress Site

Adapted from How to use jQuery 1.4 by default on your WordPress blog, add this to your functions.php / custom_functions.php to ensure you’re always using the most up-to-date version of jQuery available:

if( ! is_admin() ){
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false, '', false);
}

The version of “1” implies the latest version in the 1.x series. A version of 1.4 would have meant the latest 1.4.x version, while a version of 1.4.2 would have meant exactly version 1.4.2, even if later versions are available.

Comments

  1. Nicholas Maietta says:

    This forces extra DNS lookups by client and ISP, forces slower load times because content is also loaded from other URL’s (different part of the web), means the site you are fetching (hot linking) files from can inadvertently go down or become inaccessible from time to time, causing your site to stop functioning normally, flag’s the insecure content warning on SSL encrypted pages, potentially causes users to stray from your site. The list could go on, but why should i spend time doing that?

    Now, i dont wan to sound like a spammer, but i build a framework called …

    [Gary: Rest of this comment deleted, as it was spam – advertising a product that has no relevance to jQuery or it’s use in WordPress.]

    • Thank you for your comment Nicholas. However, you are wrong in most aspects, and I’d like to address your points.

      Firstly, while there is one extra DNS request to lookup ajax.googleapis.com, this is offset by the parallel download that a different domain can offer, which can speed up load times, not make it slower. That address is so popular for serving JavaScript Frameworks and libraries, that it will already be in ISP DNS caches and probably most user’s local DNS caches. The number of HTTP requests stays the same.

      Secondly – while there is a slim chance that ajax.googleapis.com may go down, many parts of the web would be in trouble. jQuery themselves recommend using this CDN, rather than serving locally – as various blogs do it, the performance benefit for the end user is even better, as the library is already cached from visiting other sites that make the same reference, resulting in a HTTP response of 304 Not Modified. Furthermore, as ajax.googleapis.com is a cookieless domain, the size of the HTTP request is smaller than it would be from a local site in most situations.

      Most people’s site are more likely to go down, than Google’s. If a site is using jQuery / JavaScript is a *requirement* on their site, rather than a behavioural (non-critical enhancement), then of course, a local version, or a fallback if the Google version is not loading (check to see if jQuery exists) should be done.

      The same goes for SSL pages – they have completely different needs and conditions, and it’s unlikely that serious SSL work would be done within a WordPress site (the focus of this article, as per the title).

      A reference to an off-site JavaScript file will not be causing visitors to be leaving – 90% won’t even know the external reference was made.

      So, thank you for giving me the opportunity to highlight the benefits of the code in my article.

      • Thank you for responding to my post. As far as the SSL pages and how they are served, i do agree that they have completely different needs. The CDN perspective is a good perspective and it makes perfect sense. I stand corrected ;)

  2. tzdk says:

    From this post http://paulirish.com/2009/caching-and-googles-ajax-libraries-api/ I get the idea that you should name version precisely. Or caching is not something to write home about.

Speak Your Mind

*