Wordpress - wp enqueue script using scripts from cdn with a safety callback

I've taken the previous post and done 2 things with it:

  • uses native WordPress http call functions
  • cache the fact that the CDN is up for 20 minutes (you can tweak this). You really don't want to get the CDN content on EVERY page load. No point in speeding up your site with CDNs when you do that.

$get_the_url = 'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js';

$cdnIsUp = get_transient( 'cnd_is_up' );

if ( $cdnIsUp ) {

    $load_source = 'load_external_bootstrap';

} else {

    $cdn_response = wp_remote_get( $get_the_url );

    if( is_wp_error( $cdn_response ) || wp_remote_retrieve_response_code($cdn_response) != '200' ) {

        $load_source = 'load_local_bootstrap';

    }
    else {

        $cdnIsUp = set_transient( 'cnd_is_up', true, MINUTE_IN_SECONDS * 20 );
        $load_source = 'load_external_bootstrap';
    }
 }

add_action('wp_enqueue_scripts', $load_source );

function load_external_bootstrap() { 
    wp_register_script( 'bootstrap', 'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js', array('jquery'), 3.3, true); 
    wp_enqueue_script('bootstrap'); 
}

function load_local_bootstrap() {
    wp_register_script('bootstrap', get_bloginfo('template_url').'/js/bootstrap.min.js', __FILE__, array('jquery'), 3.3, true);
    wp_enqueue_script('bootstrap'); 
}

The accepted answer is completely wrong. I suspect a gross misunderstanding of the OP'S question, since anyone with even a little experience with programming will never recommend something like "cache your own copy of the script, and perform get_url calls every 20 minutes".

@Paul G. says there is no point in loading from the CDN On every page load.

That's the whole point of using CDN'S. If you don't load from the CDN and instead load your cached copy, the client has to download it from your server. You might be in Auckland New Zealand and your client might be in London. Your server will never be able to match a CDN located closer to the client.

Furthermore, CDN'S use things like NGINX/Varnish, with massive caches that don't even touch servers. Your server may or may not be using a reverse proxy like NGINX with load balancing, it may or not have a large cache serving static files, so in order to load your copy of the files, it will have to go through the entire stack, which can be nowhere near the speed you get from loading cached static files.

Whatever you do, use CDN'S as far as possible, and yes on each and every page load.

Finally you're worried about a CDN going down. If that happens, a simple workaround would be to mention 2 or 3 other CDN'S as fallbacks, and if the primary CDN fails to load, you could easily hook up a listener and load from the others.

In the event that all the major CDN's are down, yours will not be the only site in the world that fails to work, you can add things like Facebook, Imgur, Flckr and a billion other websites that will fail with you. At that point in time, it might be better to go outside and look at the sky , and maybe you will find the UFO's that have caused the first worldwide datacenter failure in the history of mankind.

Short Answer: Use the Cdn.

Example requirejs script for fallback url's.

   requirejs.config({
   enforceDefine: true,
   paths: {
       jquery: [
        '//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min',
        //If the CDN location fails, load from this location
        //xyz.com/jquery.min.js
      ]
   }
   });

You could try something like this to test the CDN version first and then load conditionally based on if it is available or not:

$get_the_url = 'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js';
    $test_the_url = @fopen( $get_the_url,'r' );

    if ( $test_the_url !== false ) {

        function load_external_bootstrap() { 
        wp_register_script( 'bootstrap', 'http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js', array('jquery'), 3.3, true); 
        wp_enqueue_script('bootstrap'); 

    }

    add_action('wp_enqueue_scripts', 'load_external_bootstrap');

    } else {

        function load_local_bootstrap() {
        wp_register_script('bootstrap', get_bloginfo('template_url').'/js/bootstrap.min.js', __FILE__, array('jquery'), 3.3, true);
        wp_enqueue_script('bootstrap'); 
    }

    add_action('wp_enqueue_scripts', 'load_local_bootstrap');
    }