Load jquery on your WordPress blog from Google APIs

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
February 19, 2012

If your blog uses jquery, you will see a reference to the jquery file in your websites source(Ctrl+U). This jquery file could be used by one of your WordPress plugins or your theme itself to show certain content or other specific purposes. The size of the jquery file is around 90 MB and this adds up to the total size of your webpage. Wouldn’t it be great if you could load this jquery from somewhere else, from a faster source? You can load the jquery from Google Libraries to reduce the loading time of your site. Here is how you can load Jquery from Google on WordPress blogs.

The modern browsers can parallely download from upto four IPs. Therefore, if you load your jquery file from Google’s server, it will reduce the loading time of your website. Google for one is no doubt faster than your hosting. But jquery is also used by WordPress admin and it would be better to load jquery from your own server while using the admin interface. However for visitors, it is wise to load jquery from Google. For this, you will need to edit your Theme’s functions.php file. To add the code for loading jquery from Google on WordPress, open the functions.php file on your theme folder in a text editor and scroll to the bottom. Now add this code:

// loading jquery from Google for visitors

if( !is_admin()){

wp_deregister_script(‘jquery’);

wp_register_script(‘jquery’, (“http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js”), false, ‘1.7.1’);

wp_enqueue_script(‘jquery’);

}

Now save the file and upload it. Refresh your website and you’ll see that the jquery file is loaded from Google rather than from your own server. The link to the jquery script in the above code refers to the latest version of jquery as of now. If you update your WordPress, make sure to see what  version of Jquery the latest WordPress version is using and simply replace the version numbers here with the latest one. Loading jquery from Google on WordPress rather than from your own server will considerably decrease the loading time of your site.

You may also like...