Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created March 28, 2014 19:28
Show Gist options
  • Save nfreear/9841072 to your computer and use it in GitHub Desktop.
Save nfreear/9841072 to your computer and use it in GitHub Desktop.
Conditional Javascript and CSS plugin for Wordpress.
<?php
/*
Plugin Name: Conditional Javascript/ CSS
Plugin URI: https://gist.github.com/nfreear
Description: 28 March 2014.
Author: Nick Freear
Author URI: https://github.com/nfreear
Version: 0.1
*/
/*
http://stackoverflow.com/questions/11564142/wordpress-enqueue-scripts-for-only-if-lt-ie-9
http://codex.wordpress.org/Function_Reference/wp_enqueue_script
<!--[if !IE]> --> This is not IE 5-9 <!-- <![endif]-->
*/
class Conditional_Script_Plugin {
const REGEX = '@(\!?IE|(l|g)te? IE (7|8|9|10|11|12))@';
const HELP_URL = 'http://quirksmode.org/css/condcom.html';
public enqueue_conditional_script( $handle = 'html5shiv', $src = false, $cond = 'lt IE 9',
$deps = array(), $ver = false, $in_footer = false, $debug = false ) {
if (!preg_match(self::REGEX, $cond)) {
if ($debug) {
die("Syntax error in conditional comment. See, ". self::HELP_URL);
}
return false;
}
$src = $src ? $src : '//html5shim.googlecode.com/svn/trunk/html5.js';
$cc_open = '<!--[if '. $cond .']>';
$cc_close= '<![endif]-->';
if ('!IE' == $cond) {
$cc_open = '<!--[if !IE]>-->';
$cc_close= '<!--<![endif]-->';
}
add_action( 'wp_head', function() {
echo $cc_open .'<script src="'. $src .'"></script>'. $cc_close;
} );
}
}
$csp = new Conditional_Script_Plugin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment