Skip to content

Instantly share code, notes, and snippets.

@niladam
Forked from renaissance-design/functions.php
Created November 29, 2016 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niladam/f092ccf9cb8bbebd85861c7988ae11c0 to your computer and use it in GitHub Desktop.
Save niladam/f092ccf9cb8bbebd85861c7988ae11c0 to your computer and use it in GitHub Desktop.
Enqueueing jQuery from Google's CDN with local fallback in WordPress
function rd_bulletproof_jquery() {
$protocol = ($_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
$url = $protocol . '://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';
wp_deregister_script('jquery');
if (get_transient('google_jquery') == true) {
wp_register_script('jquery', $url, array(), null, true);
}
else {
$resp = wp_remote_head($url);
if (!is_wp_error($resp) && 200 == $resp['response']['code']) {
set_transient('google_jquery', true, 60 * 5);
wp_register_script('jquery', $url, array(), null, true);
}
else {
set_transient('google_jquery', false, 60 * 5);
$url = get_bloginfo('wpurl') . '/wp-includes/js/jquery/jquery.js';
wp_register_script('jquery', $url, array(), '1.7.1', true);
}
}
wp_enqueue_script('jquery');
/* Any other Javascript would be enqueued here */
}
add_action('wp_enqueue_scripts', 'rd_bulletproof_jquery');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment