How to Replace WordPress’ Default jQuery with the Google Hosted Version

WordPress comes with a version of jQuery built right in, but using that version means that new visitors to your site will need to load the file when they first visit your page. Plenty of users will have already downloaded a version of jQuery via Google’s hosted version, so we can leverage that to our advantage with a quick few lines of code in our functions.php file:

function my_scripts() {
if(!is_admin()) {
wp_deregister_script( 'jquery' );
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
}
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'my_scripts');

If you’re still not sure why you’d want to do this, I suggest reading Dave Ward’s article on the subject.

If you would prefer to load jQuery in the footer, and therefore avoid needing to have visitors download it before the rest of the page can render, you’ll want to change that call to wp_register_script(); to this:

wp_enqueue_script('jquery',
'//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',
'',
null,
true
);

Specifically, it’s the true bit there that is saying “True, load this in the footer”.

Up Next: CSS Pop-overs with Animation & Semantics