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);

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;

1 Answer 1


The order of your hooks will cause issues since your loading gavsiu_scripts via wp_enqueue_scripts and your using it to also load your fallback function using script_loader_src.

As far as I can tell the only good way to load a fallback script is to not use wp_enqueue_scripts and instead just load your js into wp_head or wp_footer, as per the example in your link.

  • But all the fallback function really does is just check to see if the file is loaded and if not, load from local source. It's done the same way the answer in the link does and jQuery isn't requested twice if the url is down. The question is really why is it requesting the validation file twice.
    – gavsiu
    Mar 26, 2013 at 0:55

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.