0

I'm trying to include selectivizr.js inside conditional comments. What am I doing wrong here?

<!DOCTYPE html>
<html class="no-js" <?php language_attributes(); ?>>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title><?php wp_title('|', true, 'right'); ?></title>
  <meta name="viewport" content="width=device-width, initial-scale=1">

    <?php wp_head(); ?>

  <!--[if (gte IE 6)&(lte IE 8)]>
  <?php
    function blarg(){
      wp_enqueue_script('selectivizr', get_template_directory_uri() . 'assets/js/vendor/selectivizr.js');
    }
    add_action('wp_enqueue_scripts', 'blarg', 101);
  ?>
  <![endif]-->

  <link rel="alternate" type="application/rss+xml" title="<?php echo get_bloginfo('name'); ?> Feed" href="<?php echo esc_url(get_feed_link()); ?>">
</head>

Nothing appears between the comments. I'm using the Roots theme. They have a scripts.php where other scripts are queued. If I put the wp_enqueue_script() in that file it works, but then I can't do the conditional comments.

UPDATE:

OK, the real challenge seems to be getting a script inside conditional comments (or any other particular HTML) will still using the queuing system. What is the best way to do that? Or is there a way?

2

Change

<!--[if (gte IE 6)&(lte IE 8)]>
  <?php
    function blarg(){
      wp_enqueue_script('selectivizr', get_template_directory_uri() . 'assets/js/vendor/selectivizr.js');
    }
    add_action('wp_enqueue_scripts', 'blarg', 101);
  ?>
  <![endif]-->

to

<!--[if (gte IE 6)&(lte IE 8)]>

<script src="<?php echo get_template_directory_uri(); ?>/assets/js/vendor/selectivizr.js" type="text/javascript"></script>

<![endif]-->

wp_enqueue_script() should only be used in a functions file.

  • +1. Technically, it's not that wp_enqueue_script() can only be used in functions.php; rather, it is that it must be called before wp_head(), inside of which the wp_enqueue_scripts action is fired. By calling it after wp_head(), it can't/won't do anything, because wp_enqueue_scripts has already fired. – Chip Bennett Mar 13 '14 at 19:05
  • I did try putting it before wp_head() but that didn't work either... I see now that I have to have the add_action(wp_enqueue_scripts...) thing inside the functions.php or something that functions.php includes I guess. But then that still places the script outside the conditional comments. So is Pieter's solution the best/only way to get it inside conditional comments? – Moss Mar 13 '14 at 19:26
2

Personally, I'd refrain from using wp_enqueue_scripts unless you're in the functions.php file.

If you're looking to call this scripts exclusively on a certain page, you can use the is_page() function. Then to detect the browser and call the function conditionally, we'll use the global variable $_SERVER['HTTP_USER_AGENT'].

function blarg(){
    if (is_page( 'Page_name' )){
        if (preg_match('/(?i)msie [6-8]/',$_SERVER['HTTP_USER_AGENT'])){
            wp_enqueue_script('selectivizr', get_template_directory_uri() . 'assets/js/vendor/selectivizr.js');
        }
    }
}   
add_action('wp_enqueue_scripts', 'blarg');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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