Created
July 23, 2012 00:18
-
-
Save benbalter/3161443 to your computer and use it in GitHub Desktop.
Fallback from CDN to origin if CDN is blocked by firewall
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Kick CDN | |
* Description: Kicks the CDN if it's unavailable | |
* Author: Benjamin J. Balter | |
* Version: 1.0 | |
*/ | |
add_action( 'wp_head', 'bb_kick_cdn', 100 ); | |
function bb_kick_cdn() { ?> | |
<script>window.onload=function(){if("undefined"==typeof jQuery){var a=document.createElement("script");a.type="text/javascript";a.onload=a.onreadystatechange=kickCDN;a.src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";document.body.appendChild(a)}};function kickCDN(){jQuery(document).ready(function(a){a.each(["src","href"],function(d,b){a("["+b+'^="http://cdn.benbalter.com"]').each(function(d,c){a(c).attr(b,a(c).attr(b).replace("http://cdn.benbalter.com","http://ben.balter.com"))})})})};</script> | |
<?php } | |
/* | |
Unminified: | |
window.onload = function () { | |
if (typeof jQuery == 'undefined') { | |
var jQ = document.createElement('script'); | |
jQ.type = 'text/javascript'; | |
jQ.onload = jQ.onreadystatechange = kickCDN; | |
jQ.src = '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; | |
document.body.appendChild(jQ); | |
} | |
}; | |
function kickCDN() { | |
jQuery(document).ready( function( $ ) { | |
var from = 'http://cdn.benbalter.com'; | |
var to = 'http://ben.balter.com'; | |
$.each( [ 'src', 'href'], function( k, attr ) { | |
$('[' + attr + '^="' + from + '"]').each( function( k, elem ) { | |
$(elem).attr( attr, $(elem).attr(attr).replace( from, to ) ); } ); | |
} ); | |
}); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment