Load jQuery from Google CDN –  WordPress function snippet

If you want to reduce the loading time of your WordPress website than this code snippet will help you out.

Personally i try to save each millisecond that i can, combining the scripts and experimenting with local and external hosting. Before i finish and publish my complete improve your website speed tutorial, i would like to share with you this simple snippet that you can insert into your functions.php file and see instant results and speed improvement.

jQuery is one of the scripts that can be found in every WordPress Installation, it has many uses and it is a favorite tool of the devs around the globe.

There is absolutely nothing wrong with having it hosted on your own server. What we are going to do is simply replace it with the updated file loaded directly from Google library so we can improve our “website speed”. One of the benefits of doing such thing is not only improvement of the page load itself but also the fact that jQuery loaded from Google library stays in the visitors browser. This simply means that if the user already visited a website that serves jQuery from google cdn, than he doesnt need to load it again and again every time new website is visited. It is an interesting fact that even though 2 websites have the same name and version of the jQuery stored localy on their server, browser still downloads them again. This will not happen if we serve the script directly from Google cdn or as the original name goes ” Google hosted libaries – for developers“.

WordPress function snippet

To replace the default script with the one from Google is a simple one function process. Open your functions.php file and copy/paste this code inside:

<?php

//Load jQuery from google
function pomeri_skripte() {
if (!is_admin()) {

wp_deregister_script('jquery');
wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js', false, '1.11.0');
wp_enqueue_script('jquery');
}
}
add_action('init', 'pomeri_skripte');
?>

What happens in this function is quite simple, we tell WordPress to ignore the default script from our local server and than register the new one directly from Google library.
I went on and run a small test on pingdom to see if it actualy makes any diference to my load time, and yes it does! Here are the load time test results.

jQuery loaded from WordPress default local library

jquery from wordpress default local library
Loading time for the default WordPress local copy of the file is 621 ms.

jQuery loaded from Google library result

jquery from google cdn

Loading time for Google library hosted copy of the file is 102 ms. That is five times faster and half a second improvement only for this one single file.

So, get busy and replace that local jQuery with the one from the Google, feel free to check the improvements using Pingdom or one of those free tools  to check your website speed.

If you like this WordPress code snippet share it with your friends and leave a comment !

Nikola Petrovic
Get in Touch

Nikola Petrovic

Hi there. My name is Nikola,
I speak English, Greek and Serbian.
As a Web Producer i can deal with almost anything web design & development related.
As a freelancer, I charge only a small portion of the money you would pay to agencies to create your website. Affordable web design doesn’t mean that it will lack any of the advanced features of today’s web technology. We just cut out the cost of the middle man.
Feel free to request a free quote using the form on the right or the one bellow.
Nikola Petrovic
Get in Touch