Last active
November 2, 2021 21:33
-
-
Save itsHall/bdfd03dfa981a1e3af1ddd1e55f2d2c4 to your computer and use it in GitHub Desktop.
Use CDN jQuery | WP
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 | |
//----------------------------------------// | |
// Making jQuery load from Google Library // | |
//----------------------------------------// | |
add_filter( 'init', 'replace_default_jquery_with_fallback'); | |
function replace_default_jquery_with_fallback() { | |
if (is_admin()) { | |
return; | |
} | |
$ver = '1.12.4'; | |
$migrateVer = '1.4.1'; | |
// Dequeue first then deregister | |
wp_dequeue_script( 'jquery' ); | |
wp_dequeue_script( 'jquery-migrate' ); | |
wp_deregister_script( 'jquery' ); | |
wp_deregister_script( 'jquery-migrate' ); | |
// Set last parameter to 'true' if you want to load it in footer | |
wp_register_script( 'jquery-core', "//ajax.googleapis.com/ajax/libs/jquery/$ver/jquery.min.js", '', $ver, false ); | |
// wp_register_script( 'jquery-mask', "//cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js", '', '1.14.10', false ); | |
wp_register_script( 'jquery-migrate', "//cdnjs.cloudflare.com/ajax/libs/jquery-migrate/$migrateVer/jquery-migrate.min.js", '', $migrateVer, false ); | |
// Fallback | |
wp_add_inline_script( 'jquery-core', 'window.jQuery||document.write(\'<script src="'.includes_url( '/js/jquery/jquery.js' ).'"><\/script>\')' ); | |
wp_add_inline_script( 'jquery-migrate', 'window.jQuery||document.write(\'<script src="'.includes_url( '/js/jquery/jquery-migrate.min.js' ).'"><\/script>\')' ); | |
wp_enqueue_script ( 'jquery-core' ); | |
// wp_enqueue_script ( 'jquery-mask' ); | |
wp_enqueue_script ( 'jquery-migrate' ); | |
} |
If the above doesn't work, use:
//----------------------------------------//
// Making jQuery load from Google Library //
//----------------------------------------//
add_filter( 'wp_enqueue_scripts', 'replace_default_jquery_with_fallback');
function replace_default_jquery_with_fallback() {
$ver = '1.12.4';
$migrateVer = '1.4.1';
// Dequeue first then deregister
wp_dequeue_script( 'jquery' );
wp_dequeue_script( 'jquery-migrate' );
wp_deregister_script( 'jquery' );
wp_deregister_script( 'jquery-migrate' );
// Set last parameter to 'true' if you want to load it in footer
wp_register_script( 'jquery', "//ajax.googleapis.com/ajax/libs/jquery/$ver/jquery.min.js", '', $ver, false );
wp_register_script( 'jquery-migrate', "//cdnjs.cloudflare.com/ajax/libs/jquery-migrate/$migrateVer/jquery-migrate.min.js", '', $migrateVer, false );
// Fallback
wp_add_inline_script( 'jquery', 'window.jQuery||document.write(\'<script src="'.includes_url( '/js/jquery/jquery.js' ).'"><\/script>\')' );
wp_add_inline_script( 'jquery', 'window.jQuery||document.write(\'<script src="'.includes_url( '/js/jquery/jquery-migrate.min.js' ).'"><\/script>\')' );
wp_enqueue_script ( 'jquery' );
wp_enqueue_script ( 'jquery-migrate' );
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for WP 5.7