Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created July 22, 2012 02:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save wpsmith/3158039 to your computer and use it in GitHub Desktop.
Save wpsmith/3158039 to your computer and use it in GitHub Desktop.
Conditionally Enqueue Script for IE browsers less than IE 9
<?php
add_action( 'wp_enqueue_scripts', 'wps_enqueue_lt_ie9' );
/**
* Conditionally Enqueue Script for IE browsers less than IE 9
*
* @link http://php.net/manual/en/function.version-compare.php
* @uses wp_check_browser_version()
*/
function wps_enqueue_lt_ie9() {
global $is_IE;
// Return early, if not IE
if ( ! $is_IE ) return;
// Include the file, if needed
if ( ! function_exists( 'wp_check_browser_version' ) )
include_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
// IE version conditional enqueue
$response = wp_check_browser_version();
if ( 0 > version_compare( intval( $response['version'] ) , 9 ) )
wp_enqueue_script( 'wps-html5shiv', 'http://html5shim.googlecode.com/svn/trunk/html5.js', array(), 'pre3.6', false );
}
@Fortyfive
Copy link

Hi, is there any way to use this to specifically target IE 8 (not less than)?
And does this just go into functions.php?

Thanks

@lordspace
Copy link

Nice snippet but that works if the person doesn't have a caching plugin installed.
If the page is served from cache that won't work for cases where the cache was triggered by a non-IE browser.

Edit:there is a discussion about including condition js using enqueue scripts but it seems it's not implemented yet (as of WP 3.9.1).
https://core.trac.wordpress.org/ticket/16024

@wpbean
Copy link

wpbean commented Aug 19, 2014

Thanks man.

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