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:
1 2 3 4 5 6 7 8 9 10 | //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.