Chandu vkm Chandu vkm - 1 year ago 95
PHP Question

Load jQuery from Google AJAX Libraries API in Wordpress with "fallback script"

I have added a Wordpress function to load jQuery Library from Google Hosted Libraries

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://", false, null);

it create the something like this

<script src="//"></script>

how can I include it with fallback option, as per html5boilerplate suggested

<script src="//"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')</script>

like this

Answer Source

Try this. Replace your code with this code. This will handle all.

/************* ENQUEUE JS *************************/

/* pull jquery from google's CDN. If it's not available, grab the local copy. */

$url = ''; // 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', ''); // 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', get_template_directory_uri().'/js/jquery.js', __FILE__, false, '1.7.2', true); // register the local file  

        wp_enqueue_script('jquery'); // enqueue the local file  

    add_action('wp_enqueue_scripts', 'load_local_jQuery'); // initiate the function  

Put this code in functions.php

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download