Load jQuery from Google CDN in WordPress

Wordpress by Cristian Labarca

WordPress, by default, includes a local copy of jQuery which gets loaded up on your website and also used by the WordPress Admin Dashboard. This is great because it makes life easier but wouldn’t it be even greater if WordPress used a copy of jQuery which was stored on Google’s CDN (or jQuery’s CDN for that matter). Well, with a tiny bit of theme editing this is possible.

Functions.php

if ( !is_admin() ) // If Not WordPress Dashboard
{
    wp_deregister_script('jquery');
    wp_register_script('jquery', ("https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"), false, '1.7.2', true);
    wp_enqueue_script('jquery');
}

There are many advantages to using jQuery from a content delivery network, it’s faster for a start because it’s likely that the script has been cached already and it puts less strain on your server. But what if, however unlikely, that Google’s CDN goes offline? Well, jQuery wouldn’t get loaded. So the bullet-proof approach would be to also add the fall-back into your theme.

Footer.php

<script> window.jQuery || document.write('<script src="js/vendor/jquery-1.7.2.min.js"></script>') </script>

This should go after your wp_footer(); function and has been taken from the HTML5 Boilerplate index file.