Skip to content

Instantly share code, notes, and snippets.

@shramee
Forked from wpscholar/functions.php
Created October 17, 2018 08:30
Show Gist options
  • Select an option

  • Save shramee/fe1fbb97b06f33182a46fa409a1c88e8 to your computer and use it in GitHub Desktop.

Select an option

Save shramee/fe1fbb97b06f33182a46fa409a1c88e8 to your computer and use it in GitHub Desktop.
Enqueueing IE conditional stylesheets in WordPress the right way
<?php
add_action( 'wp_enqueue_scripts', 'enqueue_my_styles' );
function enqueue_my_styles() {
global $wp_styles;
// Load the main stylesheet
wp_enqueue_style( 'my-theme', get_template_directory_uri() . '/style.css' );
/**
* Load our IE-only stylesheet for all versions of IE:
* <!--[if IE]> ... <![endif]-->
*/
wp_enqueue_style( 'my-theme-ie', get_template_directory_uri() . "/css/ie.css", array( 'my-theme' ) );
$wp_styles->add_data( 'my-theme-ie', 'conditional', 'IE' );
/**
* Load our IE version-specific stylesheet:
* <!--[if IE 7]><!--> ... <!--<![endif]-->
*/
wp_enqueue_style( 'my-theme-ie7', get_template_directory_uri() . "/css/ie7.css", array( 'my-theme' ) );
$wp_styles->add_data( 'my-theme-ie7', 'conditional', 'IE 7' );
/**
* Load our IE specific stylesheet for a range of older versions:
* <!--[if lt IE 9]><!--> ... <!--<![endif]-->
* <!--[if lte IE 8]><!--> ... <!--<![endif]-->
* NOTE: You can use the 'less than' or the 'less than or equal to' syntax here interchangeably.
*/
wp_enqueue_style( 'my-theme-old-ie', get_template_directory_uri() . "/css/old-ie.css", array( 'my-theme' ) );
$wp_styles->add_data( 'my-theme-old-ie', 'conditional', 'lt IE 9' );
/**
* Load our IE specific stylesheet for a range of newer versions:
* <!--[if gt IE 8]><!--> ... <!--<![endif]-->
* <!--[if gte IE 9]><!--> ... <!--<![endif]-->
* NOTE: You can use the 'greater than' or the 'greater than or equal to' syntax here interchangeably.
*/
wp_enqueue_style( 'my-theme-new-ie', get_template_directory_uri() . "/css/new-ie.css", array( 'my-theme' ) );
$wp_styles->add_data( 'my-theme-new-ie', 'conditional', 'gt IE 8' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment