How to provide a local fallback for Font Awesome if CDN fails?

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):

Read More
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.

Related posts

Leave a Reply

1 comment

  1. 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.