質問

I'm trying to develop a Wordpress theme and figure out how to provide a local fallback for Font Awesome if CDN fails or I develop my theme on a local server without internet connection.

The solution I have in mind is something like this (pseudo code):

if ( $CDN_IS_AVAILABLE ) { 
    wp_enqueue_style( 'font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css', false );
} else {
    wp_enqueue_style('font-awesome', get_template_directory_uri() . '/css/font-awesome/css/font-awesome.min.css', false, '4.0.3' );
}

Thanks.

役に立ちましたか?

解決

How about this?

<?php
    $test_url = @fopen('https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css','r');
    if ( $test_url !== false ) {
        // Use CDN  
        function load_external() {
            wp_enqueue_style( 'font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css', false );
        }
        add_action( 'wp_enqueue_scripts', 'load_external' );
    } else {
        // Use local if url is not available
        function load_local() {
            wp_enqueue_style('font-awesome', get_template_directory_uri() . '/css/font-awesome/css/font-awesome.min.css', false, '4.0.3' );
        }
        add_action( 'wp_enqueue_scripts', 'load_local' );
    }
?>

In addition to this, you could add wp_enqueue_script into the functions to do the same with JS.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top