Last active
February 9, 2016 19:54
-
-
Save bryanwillis/659053a821391e213a09 to your computer and use it in GitHub Desktop.
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 # Do not include this line if used in functions.php | |
/** | |
* WP scripts to Footer with jQuery Google CDN support. | |
* | |
* Ensures that Modernizr/HTM5shiv is loaded | |
* in the head for IE9 and less support | |
* | |
* @package Optimize_WP_Scripts_Loading | |
* @author BryanWillis | |
* @license MIT | |
* @link https://gist.github.com/bryanwillis/659053a821391e213a09 | |
*/ | |
function bw_register_jquery_google_cdn() { | |
$suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; // Allow for script debugging | |
$jquery_version = wp_scripts()->registered['jquery']->ver; // Get Wordpress jquery version | |
//* Removes Wordpress jQuery and load Google CDN version with local fallback | |
wp_deregister_script('jquery'); | |
wp_register_script( | |
'jquery', | |
'https://ajax.googleapis.com/ajax/libs/jquery/' . $jquery_version . '/jquery' . $suffix . '.js', | |
array(), | |
null, | |
true | |
); | |
add_filter('script_loader_src', 'bw_jquery_local_fallback', 10, 2); | |
//* Avoid FOUC when using modernizr-placed classes for feature-conditional styling and support html5shiv ie 9 and less | |
foreach( wp_scripts()->registered as $script ) { | |
if ( 'modernizr' == $script->handle ) { | |
wp_script_add_data( $script->handle, 'group', 0 ); | |
} else { | |
wp_script_add_data( $script->handle, 'group', 1 ); | |
} | |
} | |
} | |
add_action('wp_enqueue_scripts', 'bw_register_jquery_google_cdn', 100); | |
function bw_jquery_local_fallback($src, $handle = null) { | |
static $add_jquery_fallback = false; | |
if ($add_jquery_fallback) { | |
echo '<script>window.jQuery || document.write(\'<script src="' . $add_jquery_fallback .'"><\/script>\')</script>' . "\n"; | |
$add_jquery_fallback = false; | |
} | |
//* Optionally filter to include your own jQuery in theme folder | |
if ($handle === 'jquery') { | |
$add_jquery_fallback = apply_filters('script_loader_src', \includes_url('/js/jquery/jquery.js'), 'jquery-fallback'); | |
} | |
return $src; | |
} | |
add_action('wp_head', 'bw_jquery_local_fallback'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If using modernizr just make sure the script is registered with the handle "Modernizr" to ensure it gets loaded in the head to support IE8 and less or IE9 and less if not using a css reset. Microsoft dropped legacy browser support on January 12th so this hopefully won't be needed soon, but for now there are still around 2-5% usage of IE8 or less.