Skip to content

Instantly share code, notes, and snippets.

@maxrice
Forked from chrisguitarguy/wp-jquery.php
Created August 1, 2012 18:47
Show Gist options
  • Save maxrice/3229726 to your computer and use it in GitHub Desktop.
Save maxrice/3229726 to your computer and use it in GitHub Desktop.
Maybe load jQuery from the google cdn
add_action( 'init', 'maybe_load_jquery_from_google', 1 );
function maybe_load_jquery_from_google() {
// do not load in wp-admin
if( is_admin() )
return;
// Load from SSL if necessary
$protocol = is_ssl() ? 'https:' : 'http:';
$url = $protocol . '//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js';
$version = '1.8.1';
if ( true == get_transient( 'google_jquery_available' ) ) {
//Load jQuery from Google
wp_deregister_script( 'jquery' );
// Add version for plugins that enforce a minimum jQuery version
wp_register_script( 'jquery', $url, array(), $version );
} else {
// Check if Google CDN is available
$resp = wp_remote_head( $url );
if ( ! is_wp_error($resp) && 200 == $resp['response']['code'] ) {
// Set Google CDN as available and load jQuery
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', $url, array(), $version );
set_transient( 'google_jquery_available', true, 60 * 30 );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment