Load jQuery from Google CDN – WordPress function snippet

Home » Old posts » Load jQuery from Google CDN – WordPress function snippet

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 fivera Petrovic
Get in Touch

Nikola fivera 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.

Web Design Skills

I have been into web design for the last 10+ years.
After working for various agencies and companies I have decided to take a lone wolf path as a Freelancer but I still actively collaborate with agencies across the globe.
I enjoy building websites on WordPress platform but i can work on any other platforms as well.

SEO Skills

I have spent a last couple of years as an SEO executive and SEO consultant working on 4 continents.
Specialized in on-page SEO I wear a pointy Google-Wizard White-Hat and help various small and medium-sized businesses rank well in the search engines.
I enjoy working with Google and Yahoo SE.
Feel free to request a free meeting bellow.
As a freelancer, I charge only a small portion of the money you would pay to agencies to get your project done.
Nikola fivera Petrovic
Get in Touch
2017-04-16T04:13:51+00:00

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz