WordPress - Enqueue scripts for only if LT IE 9

前端 未结 7 628
无人及你
无人及你 2021-01-31 09:13

In a WordPress theme, how do you conditionally include scripts for ie8 and below? This is primarily to apply polyfills for various html5/css3 features. I see that wp has the $is

相关标签:
7条回答
  • 2021-01-31 10:01

    Solution for WordPress 4.2 and above

    The correct way would be to apply wp_enqueue_script since it JavaScripts here, and not css styles. Also, it is better to use get_bloginfo('stylesheet_url') to make sure this also works in Child Themes.

    /**
     * IE Fallbacks
     */
    
    function load_IE_fallback() {
        wp_register_script( 'ie_html5shiv', get_bloginfo('stylesheet_url'). '/js/html5shiv.min.js', __FILE__, false, '3.7.2' );
        wp_enqueue_script( 'ie_html5shiv');
        wp_script_add_data( 'ie_html5shiv', 'conditional', 'lt IE 9' );
    
        wp_register_script( 'ie_respond', get_bloginfo('stylesheet_url'). '/js/respond.min.js', __FILE__, false, '1.4.2' );
        wp_enqueue_script( 'ie_respond');
        wp_script_add_data( 'ie_respond', 'conditional', 'lt IE 9' );
    }
    add_action( 'wp_enqueue_scripts', 'load_IE_fallback' ); 
    0 讨论(0)
提交回复
热议问题