Skip to content

Instantly share code, notes, and snippets.

@rob-st
Last active October 20, 2019 03:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rob-st/ca7f735be23506aebcdb2676a57d2c07 to your computer and use it in GitHub Desktop.
Save rob-st/ca7f735be23506aebcdb2676a57d2c07 to your computer and use it in GitHub Desktop.
Use jQuery from a CDN in WordPress theme
<?php
function enqueue_jquery_from_cdn() {
/* Step 1: Remove the jQuery included in WP by default.
* Doc: https://codex.wordpress.org/Function_Reference/wp_deregister_script/
*/
wp_deregister_script( 'jquery' );
/* Step 2: Re-add jQuery library with another source.
* Get version source from Google CDN here:
* https://developers.google.com/speed/libraries/#jquery
* Doc: https://developer.wordpress.org/reference/functions/wp_enqueue_script/
*/
wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', '', '1.12.4' );
}
/* Step 3: Add this function to the WordPress' default script procedure.
* Doc: https://developer.wordpress.org/reference/functions/add_action/
*/
add_action( 'wp_enqueue_scripts', 'enqueue_jquery_from_cdn' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment