Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Last active March 19, 2018 20:55
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wpsmith/4083811 to your computer and use it in GitHub Desktop.
Save wpsmith/4083811 to your computer and use it in GitHub Desktop.
PHP: Enqueue Google CDN jQuery with fallback to WordPress
<?php
add_action( 'wp_enqueue_scripts', 'wps_enqueue_jquery' );
/**
* Enqueue jQuery from Google CDN with fallback to local WordPress
*
* @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script
* @link http://codex.wordpress.org/Function_Reference/wp_register_script
* @link http://codex.wordpress.org/Function_Reference/wp_deregister_script
* @link http://codex.wordpress.org/Function_Reference/get_bloginfo
* @link http://codex.wordpress.org/Function_Reference/is_wp_error
* @link http://codex.wordpress.org/Function_Reference/set_transient
* @link http://codex.wordpress.org/Function_Reference/get_transient
*
* @uses get_transient() Get the value of a transient.
* @uses set_transient() Set/update the value of a transient.
* @uses is_wp_error() Check whether the passed variable is a WordPress Error.
* @uses get_bloginfo() returns information about your site.
* @uses wp_deregister_script() Deregisters javascripts for use with wp_enqueue_script() later.
* @uses wp_register_script() Registers javascripts for use with wp_enqueue_script() later.
* @uses wp_enqueue_script() Enqueues javascript.
*/
function wps_enqueue_jquery() {
// Setup Google URI, default
$protocol = ( isset( $_SERVER['HTTPS'] ) && 'on' == $_SERVER['HTTPS'] ) ? 'https' : 'http';
// Get Latest Version
$url = $protocol . '://code.jquery.com/jquery-latest.min.js';
// Get Specific Version
//$url = $protocol . '://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js';
// Setup WordPress URI
$wpurl = get_bloginfo( 'wpurl') . '/wp-includes/js/jquery/jquery.js';
// Setup version
$ver = null;
// Deregister WordPress default jQuery
wp_deregister_script( 'jquery' );
// Check transient, if false, set URI to WordPress URI
delete_transient( 'google_jquery' );
if ( 'false' == ( $google = get_transient( 'google_jquery' ) ) ) {
$url = $wpurl;
}
// Transient failed
elseif ( false === $google ) {
// Ping Google
$resp = wp_remote_head( $url );
// Use Google jQuery
if ( ! is_wp_error( $resp ) && 200 == $resp['response']['code'] ) {
// Set transient for 5 minutes
set_transient( 'google_jquery', 'true', 60 * 5 );
}
// Use WordPress jQuery
else {
// Set transient for 5 minutes
set_transient( 'google_jquery', 'false', 60 * 5 );
// Use WordPress URI
$url = $wpurl;
// Set jQuery Version, WP stanards
$ver = '1.8.2';
}
}
// Register surefire jQuery
wp_register_script( 'jquery', $url, array(), $ver, true );
// Enqueue jQuery
wp_enqueue_script( 'jquery' );
}
@mastef
Copy link

mastef commented Sep 22, 2014

Here's an article with great advice on how to handle this correctly : http://www.hanselman.com/blog/CDNsFailButYourScriptsDontHaveToFallbackFromCDNToLocalJQuery.aspx

@moorscode
Copy link

Like the other gents were mentioning there are some flaws using the technique described here.

  1. Sure it seems fine to 'just' fetch the head request, but that's a request for every request on your site or even server.
  2. Do you really want to delay your script output the time it takes to connect to a remote host? This could be up to the timeout limit (default 2 seconds).
  3. If your server can connect to the CDN in a certain time, it is by no means a guarantee that the client will as well.

These are things you learn when working with high-volume servers and can be a server/website killer.

Which leads to a solution I came up with (which can be improved on) using a fairly common method of JavaScript fallback in the following gist: https://gist.github.com/moorscode/b04e648200c844bf3f6d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment