Bram Vanroy
Bram Vanroy

Reputation: 28505

Greater than IE8 or not IE conditional in Wordpress

Normally for conditional script loading I would write something such as this

<!--[if gt IE 8]><!-->
    <script src="<?php echo get_stylesheet_directory_uri(); ?>/js/scripts.js"></script>
<!--<![endif]-->
<!--[if lt IE 9]>
    <script src="<?php echo get_stylesheet_directory_uri(); ?>/js/scripts-ie8.min.js"></script>
<![endif]-->

However, I'd like to use Wordpress' enqueue function to make use of its dependency relations. I know that you can write conditions like this:

wp_enqueue_style( 'so27214782_ie', get_template_directory_uri() . '/ie.css', array(), '27214782', 'all' );

$wp_styles->add_data( 'so27214782_ie', 'conditional', 'IE 6' );

However, I am not sure how to apply this to my case. How do you target gt IE8 | !IE in that last statement?

Upvotes: 0

Views: 384

Answers (1)

Rich
Rich

Reputation: 926

You would put this in your functions file

wp_enqueue_script( 'scriptsa', get_template_directory_uri() . '/scriptsa.js', array(), '1.0.0', false );
add_filter( 'script_loader_tag', function( $tag, $handle ) {
    if ( $handle === 'scriptsa' ) {
        $tag = "<!--[if gt IE 8]><!-->$tag<!--<![endif]-->";
    }
    return $tag;
}, 10, 2 );

wp_enqueue_script( 'scriptsb', get_template_directory_uri() . '/scriptsb.js', array(), '1.0.0', false );
add_filter( 'script_loader_tag', function( $tag, $handle ) {
    if ( $handle === 'scriptsb' ) {
        $tag = "<!--[if lte IE 8]>$tag<![endif]-->";
    }
    return $tag;
}, 10, 2 );

Upvotes: 1

Related Questions