Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Conditionally add IE style sheets in WP
<?php
add_action( 'wp_print_styles', 'child_add_ie7_style_sheet', 200 );
/**
* Enqueue an IE-specific style sheet (for all browsers).
*
* @author Gary Jones
* @link https://garyjones.io/ie-conditional-style-sheets-wordpress
*/
function child_add_ie7_style_sheet() {
wp_enqueue_style( 'ie7', get_stylesheet_directory_uri() . '/style-ie7.css', [], '1.0' );
}
add_filter( 'style_loader_tag', 'child_make_ie7_style_sheet_conditional', 10, 2 );
/**
* Add conditional comments around IE7-specific style sheet link.
*
* @author Gary Jones & Michael Fields (@_mfields)
* @link https://garyjones.io/ie-conditional-style-sheets-wordpress
*
* @param string $tag Existing style sheet tag.
* @param string $handle Name of the enqueued style sheet.
* @return string Amended markup.
*/
function child_make_ie7_style_sheet_conditional( $tag, $handle ) {
if ( 'ie7' == $handle ) {
$tag = '<!--[if lte IE 7]>' . "\n" . $tag . '<![endif]-->' . "\n";
}
return $tag;
}
@emiluzelac
Copy link

emiluzelac commented Sep 5, 2013

Change:

add_action( 'wp_print_styles', 'child_add_ie7_style_sheet', 200 );

To:

add_action( 'wp_enqueue_scripts', 'child_add_ie7_style_sheet', 200 );

Please see: http://core.trac.wordpress.org/ticket/19510 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment