Load jQuery From Google CDN in WordPress

How to Load jQuery From Google CDN in WordPress

in Optimization & Best Practice
Google+ LinkedIn

jQuery is undoubtedly one of the most commonly used JavaScript framework these days. Some of the biggest websites out there use jQuery and there’s no question about why, it’s both easy to use and incredibly powerful. As a matter of fact, WordPress uses jQuery by default out of the box. In this article, we will show you how to replace the default WordPress jQuery script with Google hosted CDN jQuery.

There are many developer against the idea of loading jQuery from Google CDN in WordPress and many developers are in favor. The obvious reason for loading jQuery from Google CDN is website speed. It reduces website loading time because it decreases latency because jQuery script will be served by a Google server near your physical location, increases parallelism because browser will be able to download it simultaneously and provides better caching because chances are that it might have already been cached by browser from some other website. This makes loading of the jQuery script very fast on user’s end.

So if you want to reduce the loading time of your WordPress website than paste this code snippet in your functions.php file and see instant results and speed improvement. This code snippet will load jQuery v1.11.1 from Google CDN

// load jquery from google cdn in wordpress
function enqueue_jquery_google_cdn() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', false, NULL, true );
    wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_jquery_google_cdn' );

You can change URL in above example to load specific version of jQuery. Links to other versions of jQuery are available on Google Developers Library website.

But if you want to load latest version of jQuery from Google CDN then use this URL below.

http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js

Share the love

If you like this snippet, share it with others!

Leave a Reply

Your email address will not be published. Required fields are marked *