The Google Hosted Libraries is a stable, reliable, high-speed, globally available content distribution network for the most popular, open-source JavaScript libraries. Google works directly with the key stakeholders for each library effort and accepts the latest versions as they are released.
This tutorial will show you how to Load WordPress jQuery Script From Google CDN (Google Hosted Libraries). This usually is done optimize website performance and improve website load speed for relatively slow shared hosts. Use of CDN (Google Library in this case) is a great way to reduce load on your server in unexpected traffic spike as less files will be loaded from your server and load would be distributed over Google server infrastructure.
Why Google CDN for jQuery and other JavaScript libraries?
- Better performance as Google surely has a better server than you have.
- jQuery file from Google Library might be already cache in your visitors browser making to load it even faster.
- jQuery is loaded from the closest Google server.
- Less load on your server
As you can all benefits are related to website performance and most likely you read this because you are looking to improve your website performance.
How to loads WordPress jQuery Script From Google CDN
Using the following to load Google’s CDN version of jQuery, then, for the few times when (and few countries where) Google is unavailable, fall back to the copy installed with WordPress. First of all default WordPress scripts are registered via functions.php file and there are five parameters for this function.
// even more smart jquery inclusion :) add_action( 'init', 'jquery_register' ); // register from google and for footer function jquery_register() { if ( !is_admin() ) { wp_deregister_script( 'jquery' ); wp_register_script( 'jquery', ( 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' ), false, null, true ); wp_enqueue_script( 'jquery' ); } }