CDN JS Scripts Without A Plugin On WordPress

The CDNJS repository (powered by Cloudflare) claims to “host everything” (within reason based on popularity) and has faster response times than either Google or Microsoft’s much more limited content delivery networks (CDNs). There several WordPress plugins which automatically redirect script requests to CDNJS, but for the sake of optimization, there’s no point of adding another plugin when you can make the necessary adjustments in a few seconds.

Open your theme’s functions.php file and paste the following code into it.


add_action('wp_enqueue_scripts', 'CDNJS_scripts');
function CDNJS_scripts() 
{
wp_deregister_script('jquery');
wp_register_script('jquery','https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js');
wp_enqueue_script('jquery');
} 

In the example above, I’ve replaced WordPress’ original JQuery script with the minified copy at CDNJS. If you want to add additional scripts, confirm that they’re available on CDNJS, find the handle on WordPress’ Codex, and follow the pattern below (deregister the script, reregister the script pointing the handle to CDNJS’ path, and then enqueue the handle) as outlined above. Leave a comment if you have questions!

Related Articles

6 COMMENTS

    • As in outdated versions? I just manually change the link to the JQuery script (or any script, for that matter) in the code above. Currently rocking JQuery 2.1.3. =)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Try EchoTools - my free, iOS ultrasonography reference application!

Latest Articles