...

Replace default WordPress jQuery script with Google Library

91

There are default scripts that comes with each WordPress installation. These scripts are at disposal of theme and plugin authors to use in their work. From our experience, jQuery is one of the most used scripts in plugins and themes. In this article, we will show you how to replace the default WordPress jQuery script with Google Library script.

You are probably wondering why should you replace the default WordPress jQuery script with the one from Google Library? jQuery is being used on a lot of sites including the big ones. Google Library has become the standards for including scripts in web design. It could be because Google Libraries are using Google’s CDN which is extremely powerful. Because it has now become a standard, a lot of other sites are utilizing it as well. Chances are that your website user has also visited one of those other sites that are using Google’s Library for scripts. So the script is already cached in your user’s browser. This makes loading of the jQuery script very fast on user’s end. In today’s world when we are fighting for each microseconds and optimizing our site, who wouldn’t love to cut out a microsecond from the loading time?

Now you are probably wondering, how do I do this? Well its pretty simple. Open your theme’s functions.php file and paste the following snippet:

//Making jQuery Google API  function modify_jquery() {  	if (!is_admin()) {  		// comment out the next two lines to load the local copy of jQuery  		wp_deregister_script('jquery');  		wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js', false, '1.8.1');  		wp_enqueue_script('jquery');  	}  }  add_action('init', 'modify_jquery');

You are done with this. While we are talking about optimizing WordPress load time, you should also look at How WordPress plugins affect your site’s load time. Another good resource would be how to add scripts in WordPress the right way.

Note: You should not do this on theme’s or plugins that you will not be controlling forever (i.e client sites, free/commercial theme or plugin that you want to distribute to the masses). On your own site, this trick brings performance benefits. But if you don’t keep your jQuery up to date, then you run the risk of having plugin compatibility issues. You can always use a plugin: Use Google Libraries. This plugin allows your site to use common javascript libraries from Google’s AJAX Libraries CDN, rather than from WordPress’s own copies. It also keeps the libraries up to date as long as you keep that plugin up to date.

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.