741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says

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?

2063 posts
  • Has collected 50+ items on Envato Market
  • Has been part of the Envato Community for over 4 years
  • Sells items exclusively on Envato Market
  • Located in United States
Landonw says

Use the wp_print_scripts action:

add_action('wp_print_scripts','myscript');
function myscript() {
?>
<!-- IE SCRIPT -->
<?php }</pr?>
815 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Contributed a free file of the month
+3 more
rvision_ says

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.

97 posts
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Made it to the Authors' Hall of Fame
  • Located in Finland
+4 more
Smuliii says

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');
741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says

@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 :)

3243 posts
  • Has sold $5,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in United States
  • Has been part of the Envato Community for over 4 years
+1 more
organicbee says
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

97 posts
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Made it to the Authors' Hall of Fame
  • Located in Finland
+4 more
Smuliii says

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) ) { ... }
741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says

@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! :)

by
by
by
by
by
by