Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is the code im using in my functions file:

add_action('init', 'sort_out_jquery_pngfix_frontend');
function sort_out_jquery_pngfix_frontend() {
    global $wp_scripts;
    if(!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', array(), NULL, true);
        wp_register_script('dd_belatedpng', get_stylesheet_directory_uri() . '/js/dd_belatedpng.js', array(), NULL, true);
        $wp_scripts->add_data('dd_belatedpng', 'conditional', 'lt IE 7');
    }
}

add_action('wp_print_scripts', 'register_theme_scripts');
function register_theme_scripts() {
    if(!is_admin()) {
        wp_enqueue_script('modernizr', get_stylesheet_directory_uri() . '/js/modernizr-1.7.min.js', array(), NULL, false);
        wp_enqueue_script('googlemaps', 'http://maps.google.com/maps/api/js?sensor=false', array(), NULL, true);
        wp_enqueue_script('jquery');
        wp_enqueue_script('dd_belatedpng');
        wp_enqueue_script('sc_wc_js', get_stylesheet_directory_uri() . '/js/function.js', array('jquery', 'dd_belatedpng'), '1.0', true);
    }
}

I'm using $wp_scripts->add_data('dd_belatedpng', 'conditional', 'lt IE 7'); to add a conditional statement to this script as per the documentation I can find on-line but its not working. The conditional code is not shown but the js file is.

Why is this not working?

share|improve this question
3  
Why support ie 6 and keep this browser alive? WP 3.2 doesn't –  onetrickpony Jun 23 '11 at 18:45
    
Please move solution to the answer, that would be more in line with site's mechanics. –  Rarst Jun 24 '11 at 12:46
add comment

7 Answers 7

up vote 7 down vote accepted

From quick look at code this conditional only seems to be processed for styles and not scripts.

share|improve this answer
    
That's what I was going to suggest. –  Chip Bennett Jun 23 '11 at 19:04
add comment

This is the work around I had to put in place since WP doesnt support what I was trying to do

functions.php

add_action('init', 'sort_out_jquery_pngfix_frontend');
function sort_out_jquery_pngfix_frontend() {
    if(!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', array(), NULL, true);
        wp_register_script('dd_belatedpng', get_stylesheet_directory_uri() . '/js/dd_belatedpng.js', array(), NULL, true);
    }
}

add_action('wp_print_scripts', 'register_theme_scripts');
function register_theme_scripts() {
    if(!is_admin()) {
        wp_enqueue_script('modernizr', get_stylesheet_directory_uri() . '/js/modernizr-1.7.min.js', array(), NULL, false);
        wp_enqueue_script('googlemaps', 'http://maps.google.com/maps/api/js?sensor=false', array(), NULL, true);
        wp_enqueue_script('jquery');
        wp_enqueue_script('sc_wc_js', get_stylesheet_directory_uri() . '/js/function.js', array('jquery'), '1.0', true);
    }
}

footer.php

<?php wp_footer(); ?>
<!--[if lt IE 7]>
<?php wp_print_scripts(array('dd_belatedpng')); ?>
<script>DD_belatedPNG.fix("img, .png_bg");</script>
<![endif]-->
share|improve this answer
    
+1 for having a working solution, but wired anyway... –  kaiser Jun 28 '11 at 15:18
add comment

It is a long shot, but you might try registering the script, then adding in the conditional, and then enqueueing the script:

// Register the script
wp_register_script( 'dd_belatedpng', get_stylesheet_directory_uri() . '/js/dd_belatedpng.js', array(), NULL, true );
// Attempt to add in the IE conditional tags
$wp_scripts->add_data('dd_belatedpng', 'conditional', 'lt IE 7');
// Enqueue the script
wp_enqueue_script( 'dd_belatedpng' );

I don't know that it will work, though

EDIT

Based on the related Trac ticket, it appears $wp_scripts doesn't support this method.

You may just need to pull the script out of the wp_enqueue_script() system, and echo the IE conditional-enclosed script call inside of a pluggable function hooked into wp_print_scripts or wp_head. It's certainly not ideal, but if this is a single-use client Theme, then you don't have to worry about someone else needing to deregister the script.

share|improve this answer
    
Thanks Chip I will try that tomorrow when I'm back at work. I will make one amend though and that's to put the wp_register_script and add_data in an init hook rather than the wp_print_scripts hook. –  Brady Jun 23 '11 at 20:17
    
@Brady As far as I could see from the file I took as reference, init works perfect for register. @Chip Bennet As this is just so genious!: +1 :) –  kaiser Jun 23 '11 at 20:53
    
updated question with new code trying your suggestion Chip and it doesnt work :( –  Brady Jun 24 '11 at 8:19
    
updated answer with a less-than-ideal, but not absolute-worst-case, option. –  Chip Bennett Jun 24 '11 at 12:31
add comment

Just found a partial solution for this via $is_IE in wp-includes/vars.php!

function emporium_enqueue_scripts() {
    global $is_IE;
    if( $is_IE ) {
        wp_register_script( 'emporium-focus' , get_template_directory_uri() . '/library/focus.js', '', '',  true );
        wp_enqueue_script( 'emporium-focus' );
    }
}
add_action('init', 'emporium_enqueue_scripts');

That seems to load library/focus.js on an if IE basis but there's nothing in core to do any IE version conditionals for scripts. Looks like a Trac ticket on the subject has been shelved for the time being.

share|improve this answer
    
Thanks for the input and idea. Though is_IE will target all IE versions not just IE6. –  Brady Jun 24 '11 at 12:34
add comment

It looks like you are putting it in the footer. Did you look at the end of the page for the JS file?

share|improve this answer
    
Yes I looked and its not that. –  Brady Jun 23 '11 at 20:15
add comment

This works for scripts too. But only in one situation: If the script was registered first. You can't go straight for enqueue. You'll have to do: register -> add_data -> enqueue. This is both for scripts as for styles the same rule.

share|improve this answer
    
Isn't that what I just said? :) –  Chip Bennett Jun 23 '11 at 19:57
1  
Oh, ehm... yea. If this is wrong I will downvote you ;) –  kaiser Jun 23 '11 at 20:05
    
As you should! :) –  Chip Bennett Jun 23 '11 at 20:24
    
I've tried it in this order and it doesnt work. See edited code in my question. –  Brady Jun 24 '11 at 8:21
add comment

You should add script version in your code...

More details here http://codex.wordpress.org/Function_Reference/wp_enqueue_script

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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