Подтвердить что ты не робот

Загрузите jQuery из API-интерфейсов API AJAX Google в Wordpress с помощью "fallback script"

Я добавил функцию Wordpress для загрузки библиотеки jQuery из размещенных в Google библиотек

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
}

он создает что-то вроде этого

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

как я могу включить его с резервной опцией, в соответствии с предложенным html5boilerplate

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

как это

4b9b3361

Ответ 1

Попробуйте это. Замените код на этот код. Это будет обрабатывать все.

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

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

$url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/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.9.1/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', 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  
}  

Поместите этот код в functions.php

Ответ 2

Answer для @Mangesh Parte может быть короче, например

<?php
    function load_external_jQuery() {
        wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery 
        $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'; // the URL to check against  
        $test_url = @fopen($url,'r'); // test parameters  
        if( $test_url !== false ) { // test if the URL exists if exists then register the external file  
            wp_register_script('jquery', $url);
        }
        else{// register the local file 
            wp_register_script('jquery', get_template_directory_uri().'/js/jquery.js', __FILE__, false, '1.7.2', true);  
        }
        wp_enqueue_script('jquery'); // enqueue the jquery here
    }
    add_action('wp_enqueue_scripts', 'load_external_jQuery'); // initiate the function 
?>

Ответ 3

Вы можете попробовать следующее:

<script>
try { 
        //First check if jQuery exists..
    var test = jQuery.trim("test");
} catch(e) {
        //If it doesn't, add it manually..
    var script = document.createElement("SCRIPT");
    script.setAttribute("src", "js/vendor/jquery-1.9.1.min.js");
    document.head.appendChild(script);
}
</script>

Ответ 4

Почему много ответов с серверным решением? Подумайте о том, кто живет в Китае и Google заблокирован.

Вот правильный ответ (без серверной):

Для Wordpress 4.5.0 +: wp_add_inline_script() #

function jquery_enqueue_with_fallback() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery' , '//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js', false, '3.1.0', true );
    wp_add_inline_script( 'jquery', 'window.jQuery||document.write(\'<script src="'.esc_url(get_template_directory_uri()).'/libs/js/jquery.js"><\/script>\')' );
    wp_enqueue_script( 'jquery' );
}
add_action( 'wp_enqueue_scripts' , 'jquery_enqueue_with_fallback' );
  • Нет серверной проверки
  • Скрипты внизу
  • Реальный резерв (для локального script)

Примечание. Измените версию и свой собственный локальный источник jQuery.