Ever wanted to change how Word­Press calls jquery?

There are many ben­e­fits to this, the largest being that as its hosted at a CDN, you get the ben­e­fits the speed of that CDN, as well as caching if the user has already loaded the same script from Google before. That means the user won’t even have to down­load it! This will help speed up your site, which is always a good thing. There are a few ways to do this, but I’m going to show you the Word­Press “approved” way:

// Remove the default jQuery script
wp_deregister_script('jquery');
// Register the google hosted version
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js', '', '', true );
wp_enqueue_script( 'jquery' );

Ok, so lemme explain what’s going on here. First off, I’m putting this code in header.php right before my call to the wp_head function.

Word­Press loads its own ver­sion of jquery, so first off we are going to “dereg­is­ter” it with the wp_deregister_script func­tion. Sec­ondly, we are going to reg­is­ter the Google ver­sion using wp_register_script. We’ll reg­is­ter it with the name of jquery and the path to the Google ver­sion we want to call. Also, I’m forc­ing the last para­me­ter in this func­tion to true. This tells Word­Press to load this script in the footer, so we aren’t block­ing any­thing else being loaded.

Finally, we’ll dump the js call into the page using wp_enqueue_script. And that’s it! You should be load­ing jquery in the bot­tom of your site now, from google.