How to Load JavaScript Libraries from Google CDN & Why It’s Not Recommended

Tons of articles smartly tell us that we should add the following piece of code to theme’s functions.php file in order to harness the advantage of Google CDN:

//dequeue jQuery from any plugin
 add_action( 'wp_enqueue_scripts', 'dequeue_jquery', 999999 );
//enqueue jQuery from Google CDN
 function dequeue_jquery() {
 wp_deregister_script( 'jquery' );
 wp_register_script( 'jquery', '//');
 wp_enqueue_script( 'jquery' );

This code snippet will dequeue jQuery library from WordPress, and enqueue Google hosted jQuery.

More resources:


Why Use Google’s CDN?

The three most commonly cited reasons to dequeue WordPress’ bundled version of jQuery in favor of Googles are:

  1. Decreased Latency
  2. Increased Parallelism
  3. Better Caching

Why NOT Use Google’s CDN?

WordPress takes great care to make sure its bundled libraries are fully compatible with other bundled scripts.  jQuery is loaded in “no conflict” mode to prevent any potential script collisions over the $ variable in the global namespace.  As of version 3.6, WordPress will be shipping with version 1.9 of jQuery – which removes several deprecated APIs like .live().  To prevent things from breaking, WordPress automatically loads jQuery Migrate, a plugin that adds these removed APIs back to the system, but alerts you in the browser console of their deprecation.

Read more: Don’t Dequeue WordPress’ jQuery

This entry was posted in WordPress. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *