I'm trying to modify this to register and enqueue a jQuery plugin from a CDN with local fallback.
However, when the URL fails (or if I give it a fake url) Firebug receives 2 aborted calls to the URL before loading the local fallback. With the jQuery fallback like the link, it only receives 1 aborted call.
It looks like it is trying to load the script twice before using the fallback.
function gavsiu_scripts() {
if (is_single() && comments_open() && get_option('thread_comments') || is_page_template('page-contact.php')) {
wp_register_script('jquery_validate', '//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js', array('jquery'), null, false);
add_filter('script_loader_src', 'gavsiu_jquery_validate_local_fallback', 10, 2);
wp_enqueue_script('jquery_validate');
}
}
add_action('wp_enqueue_scripts', 'gavsiu_scripts', 100);
function gavsiu_jquery_validate_local_fallback($src, $handle) {
static $add_fallback = false;
if ($add_fallback) {
echo '<script>if(typeof $().validate == \'undefined\') document.write(\'<script src="' . get_template_directory_uri() . '/js/jquery.validate.min.js"><\/script>\')</script>' . "\n";
$add_fallback = false;
}
if ($handle === 'jquery_validate') {
$add_fallback = true;
}
return $src;
}