Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Last active August 25, 2018 11:44
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save GaryJones/1698719 to your computer and use it in GitHub Desktop.
Save GaryJones/1698719 to your computer and use it in GitHub Desktop.
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

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