Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question

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.

share|improve this answer
    
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 '13 at 0:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.