How do you add an IE only scripts to WP theme? I want to avoid putting the script code directly in theme header but can’t see any other way. How do you guys do it?
Use the wp_print_scripts action:
add_action('wp_print_scripts','myscript');
function myscript() {
?>
<!-- IE SCRIPT -->
<?php }</pr?>With conditional tags:
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
This one is for html 5 support, goes for all IE versions lower than 9.
You could use Wordpress own global variable called $is_IE:
function yourtheme_register_scripts() {
if(!is_admin()) {
wp_register_script('selectivizr', get_template_directory_uri().'/js/selectivizr-min.js', array('jquery'), false, true);
}
}
add_action('init', 'yourtheme_register_scripts');
function yourtheme_enqueue_scripts() {
if(!is_admin()) {
// For Internet Explorer
global $is_IE;
if($is_IE) {
wp_enqueue_script('selectivizr');
}
}
}
add_action('wp_print_scripts', 'yourtheme_enqueue_scripts');
@Landon not bad but I’d still like to register and enqueue scripts the proper way with dependencies etc.
@rvision_ that’s exactly what I want to avoid, I know how to add conditional scripts but want to use the proper way with wp_register_script and wp_enqueue_script
@Smuliii your answer is the closest to what I need. Any ideas how to make the scripts conditional based on the version too? I don’t want to load all this shit for IE9 since IE9 seems to handle most of the things pretty well (for IE)
Anyway, thanks for your answers, looking for more to come 
pogoking said
@Landon not bad but I’d still like to register and enqueue scripts the proper way with dependencies etc.@rvision_ that’s exactly what I want to avoid, I know how to add conditional scripts but want to use the proper way with wp_register_script and wp_enqueue_script
@Smuliii your answer is the closest to what I need. Any ideas how to make the scripts conditional based on the version too? I don’t want to load all this shit for IE9 since IE9 seems to handle most of the things pretty well (for IE)
Anyway, thanks for your answers, looking for more to come![]()
remember theres a built in function in WordPress to add conditional CSS , not currently for for scripts but it looks like they may be adding it in 3.3 http://core.trac.wordpress.org/ticket/16024
If you want to detect if user is using either IE7 or IE8 , you could try this one:
function detect_ie($ie7_check = true, $ie8_check = true) {
$ie7 = ($ie7_check == true) ? strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.0') : false;
$ie8 = ($ie8_check == true) ? strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8.0') : false;
if ($ie7 !== false || $ie8 !== false) {
return true;
} else {
return false;
}
}
// IE7 & IE8
if( detect_ie() ) { ... }
// IE7
if( detect_ie(true, false) ) { ... }
// IE8
if( detect_ie(false) ) { ... }
@OrganicBeeMedia I know about conditional styles, hope they will add the same for scripts soon 
@Smuliii nice, I’ll try your method. Too bad WordPress doesn’t have something built in yet. Thanks a lot! 
