Skip to content

Instantly share code, notes, and snippets.

@painedpineapple
Created October 11, 2016 17:28
Show Gist options
  • Save painedpineapple/9be4e79291335cb6fc15b576d4868692 to your computer and use it in GitHub Desktop.
Save painedpineapple/9be4e79291335cb6fc15b576d4868692 to your computer and use it in GitHub Desktop.
function cdnExternalOrLocal ($url) {
$cdnIsUp = get_transient('cdn_is_up');
$load_source;
if ($cdnIsUp) {
$load_source = 'external';
} else {
$cdn_response = wp_remote_get($url);
if( is_wp_error( $cdn_response ) || wp_remote_retrieve_response_code($cdn_response) != '200' ) {
$load_source = 'local';
}
else {
$cdnIsUp = set_transient( 'cnd_is_up', true, MINUTE_IN_SECONDS * 20 );
$load_source = 'external';
}
}
return $load_source;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment