In HTML5 Boilerplate it Grab Google CDN's jQuery with this JS code

<!-- Grab Google CDN's jQuery, with a protocol relative URL; fall back to local if offline -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.1.min.js"><\/script>')</script>

I tried to transpose it to WP with this PHP code

function sbi_cdn_jquery()
    global $wp_scripts;


    echo '<script src="//ajax.googleapis.com/ajax/libs/jquery/' . $wp_scripts->registered['jquery']->ver . '/jquery.min.js"></script>';
    echo '<script>window.jQuery || document.write(\'<script src="' . get_bloginfo('wpurl') . '/wp-includes/js/jquery/jquery.js"<\/script>\')</script>';
add_action( 'wp_enqueue_scripts', 'sbi_cdn_jquery' );

But this does not work !

Is this possible to fallBack Google CDN in JavaScript with WP ?

Thank for your help.

  • Check this out: github.com/sgarbesi/fallback.js It's a library that I wrote that allows you to add as many fallback CDN's as you want. – user33360 May 28 '13 at 3:02

Scripts don't get printed at the wp_enqueue_scripts hook, but rather at the wp_print_scripts hook. That said: don't echo/print scripts, period. Instead, enqueue them properly.

Caveat: This method is Plugin territory, and should not be included as Theme code in a publicly distributed Theme.

You deregister jQuery, but the step you're missing is registering and enqueueing your own version:

function sbi_cdn_jquery() {
    // Deregister core-bundled jQuery
    // This is dangerous! Understand the
    // potential consequences before doing this!
    wp_deregister_script( 'jquery' );

    // Register custom jQuery version
    wp_register_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' );

    // Enqueue cusotm jQuery
    wp_enqueue_script( 'jquery' );
add_action( 'wp_enqueue_scripts', 'sbi_cdn_jquery' );

Refer to the Codex entry for wp_register_script() and wp_enqueue_script() regarding arguments for these functions.

  • Excellent answer, with only one question. Wouldn't it be better to add if ( ! is_admin() ) before you deregister jQuery? I've always assumed it was unwise to swap in your version of jQuery for the admin area as it could potentially cause unwanted issues. – cmegown May 23 '12 at 20:14
  • 1
    Absolutely. My answer isn't comprehensive regarding all best practices with respect to enqueueing scripts - primarily because the premise of the question itself goes against best practice. – Chip Bennett May 23 '12 at 20:23
  • Thank for your answer. But if Google CDN does not respond, how can i load local WP Jquery (fall back to local if offline) ? – LeMoussel May 24 '12 at 19:00
  • 1
    @ChipBennett He is trying to have a fallback mechanism just like we have in HTML5 Boilerplate. Having a fallback is the best way among any other way. – Ashish Kumar - Ashfame May 25 '12 at 7:50
  • 1
    @Ashfame: I assume anyone advanced enough to know the risks and caveats with this approach would also be advanced enough not to need to ask a basic question about script enqueueing. – Chip Bennett May 31 '12 at 21:34

I use this function to register and enqueue the various scripts my theme uses:

function cosmos_enqueue_scripts() {

// Load jquery from Google CDN (protocol relative) with local fallback when not available
if ( false === ( $url = get_transient('jquery_url') ) ) {

    // Check if Google CDN is working
    $url = ( is_ssl() ? 'https:' : 'http:' ) . '//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
    $resp = wp_remote_head($url);

    // Load local jquery if Google down
    if ( is_wp_error($resp) || 200 != $resp['response']['code'] ) {

        $url = get_template_directory_uri() . '/js/vendor/jquery-1.7.2.min.js';

    // Cache the result for 5 minutes to save bandwidth
    set_transient('jquery_url', $url, 60*5);

// Deregister Wordpress' jquery and register theme's copy in the footer
wp_register_script('jquery', $url, array(), null, true);

// Load other theme scripts here
wp_enqueue_script('modernizr', get_template_directory_uri() . '/js/vendor/modernizr-2.5.3.min.js', array(        ), null, false);
wp_enqueue_script('bootstrap', get_template_directory_uri() . '/js/vendor/bootstrap.min.js'      , array('jquery'), null, true );
wp_enqueue_script('main'     , get_template_directory_uri() . '/js/main.js'                      , array('jquery'), null, true );
add_action('wp_enqueue_scripts', 'cosmos_enqueue_scripts');
  • This will slow things down a bit and moreover only checks when the PHP actually ran and not everytime when the cached pages are served. Best is to do it on client side by checking if jQuery has been defined just after loading it. – Ashish Kumar - Ashfame May 25 '12 at 7:48
  • Good point about caching. I hadn't thought of that. – nautilus7 May 25 '12 at 8:08

Posted the same answer on jquery stopped working after deregistering wordpress stock version, but adding here for reference:

If you're stuck on using a Google hosted version of jQuery, there are two plugins that come to mind:

It's better to just use the core if you're not 100% comfortable with JavaScript debugging (if anything pops up as it did) and do not control the environment completely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.