1

I am trying to enqueue a conditional javascript file for lt IE9 in the functions.php file (in Wordpress). For anyone viewing the website in IE9 this script will be triggered. Is what I have below anywhere near correct? That jQuery $ will cause problems because of Wordpress runs in nonConflict mode. But this was the only answer I have found. I already have multiple style sheets and scripts running in my functions.php file with no bother.

global $wp_scripts;
wp_register_script( 'ie_js', '/js/ie.js,  array(),  '' );
$wp_scripts->add_data( 'ie_js', 'conditional', 'lt IE 9' );

1 Answer 1

0

Yes, that looks broadly correct (barring a missing '). That isn't a jQuery $, so shouldn't cause any problems - the dollar prefix here is the usual convention for PHP variable names and won't make it into the included script.

Or if I've misunderstood and you meant your ie.js script assumes jQuery is $ then you can solve that by wrapping it in a self-executing function which defines a scope where $ = jQuery, e.g.

(function($){
    ...
})(jQuery);

Also as of 4.2 there's a helper function wp_script_add_data so you can write instead:

wp_register_script( 'ie_js', '/js/ie.js',  array(),  '' );
wp_script_add_data( 'ie_js', 'conditional', 'lt IE 9' );
3
  • function every_theme_js() { wp_enqueue_script( 'header_javascript_js', get_template_directory_uri() . '/js/header_javascript.js',array('jquery'), '', true ); wp_register_script( 'ie_js', '/js/ie.js', array(), '' ); wp_script_add_data( 'ie_js', 'conditional', 'lt IE 9' ); } add_action('wp_enqueue_scripts', 'every_theme_js' ); Commented Aug 10, 2016 at 20:37
  • Would the above code be correct or do I need to add a second function for the conditional script? You're a gentleman thanks a million Commented Aug 10, 2016 at 20:38
  • Sorry for the slow reply. Yes, putting everything into one action is fine!
    – Rup
    Commented Aug 12, 2016 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.