Use Google-Hosted jQuery in Your WordPress Theme

How many sites use popular JavaScript libraries like jQuery? A lot. That’s why Google hosts many of them on their speedy CDN, so browsers only have to download jQuery or Prototype once in a day, instead of once per site.

How can your WordPress-powered site benefit from this? Digging into WordPress has the answer:

if ( !is_admin() ){
   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_enqueue_script('jquery');
}

This little snippet goes in your functions.php, where it deregisters WordPress’s internal copy of jQuery and references Google’s. Unfortunately, it’s not set up for the handy no-conflict mode that lets you use Prototype scripts on the same blog.

  • http://blog.milandinic.com/ Milan

    This is not a right way to use Google-hosted jQuery in WordPress. You should simply install plugin Use Google Libraries which would automatically load jQuery from Google in noConflict mode and also load other libraries that are available from Google like jQuery UI, Prototype and SWFObject, without WordPress’ version added at the end of URL..

    And you don’t have to edit any file to add any code!

    • http://www.webmaster-source.com Matt

      Ah, I forgot about noConflict mode. Thanks for the plugin link.

  • http://www.articlesur.com Quang

    Nice post. And use following code if you are working offline:

    window.jQuery || document.write(”)

    :)