Last active
October 20, 2019 03:05
-
-
Save rob-st/ca7f735be23506aebcdb2676a57d2c07 to your computer and use it in GitHub Desktop.
Use jQuery from a CDN in WordPress theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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