Ever wanted to change how WordPress calls jquery?
There are many benefits to this, the largest being that as its hosted at a CDN, you get the benefits 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 download 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 WordPress “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.
WordPress loads its own version of jquery, so first off we are going to “deregister” it with the wp_deregister_script function. Secondly, we are going to register the Google version using wp_register_script. We’ll register it with the name of jquery and the path to the Google version we want to call. Also, I’m forcing the last parameter in this function to true. This tells WordPress to load this script in the footer, so we aren’t blocking anything else being loaded.
Finally, we’ll dump the js call into the page using wp_enqueue_script. And that’s it! You should be loading jquery in the bottom of your site now, from google.
Leave a Reply