Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - Enqueue script only for IE

Tags:

wordpress

I want to use dd belatedpng so the PNG's on my website appear properly on IE. The script I've always used on non-wordpress websites was

<!--[if lt IE 7 ]>
    <script src="js/dd_belatedpng.js"></script>
    <script> DD_belatedPNG.fix('img, .ir'); </script>
<![endif]-->

Now that I need to use it on a Wordpress website, I'm trying to find a way of adding that script using wp_enqueue_script (although I don't like that system at all). At the end of the day, the theme is only going to be used on a single website, I'd prefer to hardcode the scripts path.

Anyway, is there a way of adding IE conditionals to enqueue script and or register script?

like image 788
José Tomás Tocino Avatar asked Jan 08 '11 14:01

José Tomás Tocino


People also ask

How do I add enqueue scripts in WordPress?

To enqueue scripts and styles in the front-end you'll need to use the wp_enqueue_scripts hook. Within the hooked function you can use the wp_register_script() , wp_enqueue_script() , wp_register_style() and wp_enqueue_style() functions.

How do I enqueue links in WordPress?

Notes. The function should be called using the wp_enqueue_scripts action hook if you want to call it on the front-end of the site, like in the examples above. To call it on the administration screens, use the admin_enqueue_scripts action hook. For the login screen, use the login_enqueue_scripts action hook.

How do I enqueue a script in a child theme?

The recommended way of enqueuing the stylesheets is to add a wp_enqueue_scripts action and use wp_enqueue_style() in your child theme's functions.php . If you do not have one, create a functions.php in your child theme's directory.

How do I enqueue a script in the footer in WordPress?

To add the script in the footer or bottom of a WordPress page, all you need to do is set the $in_footer parameter to true . We have also used another function get_template_directory_uri() which returns the URL for the template directory.


1 Answers

The browser detection is built into WordPress with the global variable $is_IE so...

<?php
global $is_IE;
if ( $is_IE ) {
    wp_enqueue_script( 'dd_belatedpng', bloginfo('template_directory').'/js/dd_belatedpng.js' );
}
?>

For the actual script you want to execute, you should probably add it to another file that is enqueued with dd_belatedpng as a dependency.

like image 131
Taylor Dewey Avatar answered Nov 03 '22 04:11

Taylor Dewey