741 posts
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Poland
  • Referred between 10 and 49 users
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?

2032 posts
  • Has been a member for 4-5 years
  • Bought between 50 and 99 items
  • Exclusive Author
  • United States
Landonw says

Use the wp_print_scripts action:

add_action('wp_print_scripts','myscript');
function myscript() {
?>
<!-- IE SCRIPT -->
<?php }</pr?>
814 posts
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • Serbia
  • Has been a member for 5-6 years
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
  • Bought between 10 and 49 items
  • Elite Author
  • Exclusive Author
  • Finland
  • Has been a member for 5-6 years
  • Referred between 100 and 199 users
  • Sold between 250 000 and 1 000 000 dollars
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
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Poland
  • Referred between 10 and 49 users
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 :)

3192 posts
  • Sold between 5 000 and 10 000 dollars
  • United States
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
  • Exclusive Author
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
  • Bought between 10 and 49 items
  • Elite Author
  • Exclusive Author
  • Finland
  • Has been a member for 5-6 years
  • Referred between 100 and 199 users
  • Sold between 250 000 and 1 000 000 dollars
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
  • Has been a member for 5-6 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Poland
  • Referred between 10 and 49 users
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