0

This makes no sense, but in looking at my view source across browsers, wp_localize_script() is inserting tags as expected for all browsers other than IE7,8,9. The script tag is simply not there in those browsers. I have it bound to "jquery", since I want to make sure it's included no matter what (rather than binding it to any specific user-defined script). It's being called in my theme's functions.php file.

The documentation for wp_localize_script seems to suggest that the script must be registered before calling this function. Given that the function is always being called at the same place, it seems that perhaps jQuery is being enqueued or registered at different times depending on browser? That seems very odd indeed, as WordPress doesn't natively do a lot of browser detection out of the box.

I can get wp_localize_script working if I bind it to one of my custom enqueued scripts and make sure the function call is immediately after that script is enqueued. This is why I suspect it has something to do with the queueing order of jQuery.

Can anyone suggest what might be going on here?

2
  • What are you using wp-localize-script for? This is usually for language localization or just to pass an object of data into the script for use within. Commented Nov 9, 2011 at 15:38
  • The latter - global data, like the installation-relative URI of some resources.
    – Tom Auger
    Commented Nov 11, 2011 at 14:27

2 Answers 2

1

I do it this way and it works fine. Make sure you don't have jQuery defined anywhere else on your site (like manually in the header), as some browsers don't like that.

/**
 * Load assets on initiation
 */
add_action('init', 'load_assets');
function load_assets(){
    wp_enqueue_scripts('jquery');
}
3
  • Hi David. No offense, but that's just plain silly. jQuery is added by default within the wp_head() function that is generally called automatically by most themes (certainly by the WordPress bundled themes). It happens before the wp_enqueue_scripts action. Putting it in init won't actually do anything, other than just "remind" WordPress that it needs to add it during wp_head(), which it already does. You're just muddying up your code there.
    – Tom Auger
    Commented Nov 11, 2011 at 14:30
  • 1
    Actually unless it is explicitly set, WordPress won't include jQuery by default. Maybe you're using a theme that already enqueues it, but nothing is enqueued by default. And also, you shouldn't be localizing jQuery as jQuery is just a js framework and you should keep your actual js code in a separate file. So, no, hes not just muddying up any code, it sounds like you are not creating this theme from scratch and therefore are unaware of what WordPress does by default. Commented Nov 11, 2011 at 15:03
  • 1
    @KevinLangleyJr. - You are correct, I am using a custom theme, so I need to enqueue JS myself.
    – David Gard
    Commented Nov 11, 2011 at 17:02
0

This is how to load latest jQuery - no matter what.

wp_deregister_script( 'jquery' );
wp_register_script('jquery', 'http://code.jquery.com/jquery-1.7.min.js',array(),'1.7');
wp_enqueue_script('jquery');

Hope this helps. codeFX.biz

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.