1

I'm sure you're all aware of loading files like jquery off google. I'm trying to load a file similarly, but I want to have a backup of that file on my server incase the online version is down, and I don't know how to go about that in Wordpress.

Here's what I have currently which loads it from the url

wp_deregister_script('html5shiv');
wp_register_script('html5shiv', ("http://html5shim.googlecode.com/svn/trunk/html5.js"),false);
wp_enqueue_script('html5shiv');
Marius Miliunas
  • 1,023
  • 18
  • 34

2 Answers2

2

I think this is what you are looking for.

C&P in case someone came back later looking for this and the site is unavailable:

<?php
$url = 'http://ajax.googleapis.com/ajax/libssss/jquery/1.6.4/jquery.min.js'; // the URL to check against
$test_url = @fopen($url,'r'); // test parameters
if($test_url !== false) { // test if the URL exists
    function load_external_jQuery() { // load external file
        wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'); // register the external file
        wp_enqueue_script('jquery'); // enqueue the external file
    }
    add_action('wp_enqueue_scripts', 'load_external_jQuery'); // initiate the function
} else {
    function load_local_jQuery() {
        wp_deregister_script('jquery'); // initiate the function
        wp_register_script('jquery', bloginfo('template_url').'/js/libs/jquery-1.6.1.min.js', __FILE__, false, '1.6.4', true); // register the local file
        wp_enqueue_script('jquery'); // enqueue the local file
    }
add_action('wp_enqueue_scripts', 'load_local_jQuery'); // initiate the function
}
?>
Eduardo Reveles
  • 2,155
  • 17
  • 14
  • @MariusMiliunas - I would do a client-side solution instead of a server side solution. This only checks whether the jQuery library is available to YOUR server, not to your client. Places like Iran block access to Google's CDN, so your site won't work for them. Perhaps Iran isn't your typical visitor, but I think you get my point. – mrtsherman Feb 11 '12 at 05:14
0

I'm not familiar with the Wordpress queuing system, but here is some general code for javascript with fallback. You may want to post this question at http://wordpress.stackexchange.com where WP api questions tend to get better answers. Here is a fallback mechanism

//add a property to the window object in foo.js
window.banana = 'peeled';

<head>
  <script src="https://mysite.com/foo.js" type="text/javascript"></script>

  <script type="text/javascript">    
    //fallback mechanism if not available    
    if (!window.Banana) { document.write(unescape("%3Cscript src='/localfoo.js' type='text/javascript'%3E%3C/script%3E")); }

  </script>
</head>
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • reading the comments from the previous posts link I came across something similar to this here http://beneverard.co.uk/blog/wordpress-loading-jquery-correctly-version-2/ – Marius Miliunas Feb 11 '12 at 04:04