WordPress: How to enqueue jQuery from Google’s CDN without a plugin

Prefer to load jQuery from Google rather than enqueue it from WordPress’ Core?

Yeah, we do too. There are many benefits to loading jQuery from Google’s CDN (Content Delivery Network):

  • It’s likely a user previously visited a site that relied on the jQuery version your calling, which means they’ll have it cached already.
  • If they don’t have it cached, no biggie – Google will serve it to the browser faster than your server.
  • The http request is coming from Google and not your domain, so the browser can fetch the resource while continuing to focus on getting other assets simultaneously.

Just drop this snippet in your theme’s functions.php file and change the jQuery version number if you’d like. Bada bing.

function kickass_enqueue_method() {
if (!is_admin()) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
wp_enqueue_script( 'jquery' );

add_action('init', 'kickass_enqueue_method');

No Comments

Leave a Reply

To include code, just include it in [code] [/code] square brackets. Sweet.