[wp-hackers] wp_localize_script - when should the JS var be available

Eric Andrew Lewis eric.andrew.lewis at gmail.com
Sat Apr 26 17:25:42 UTC 2014


Any localized script has the data output directly preceding the related script element, via the print_extra_script() method in WP_Scripts (https://github.com/WordPress/WordPress/blob/master/wp-includes/class.wp-scripts.php#L127).  

--  
Eric Andrew Lewis


On Friday, April 25, 2014 at 6:05 PM, J.D. Grimes wrote:

> I ask because I noticed an "undefined variable ‘settings’” notice in the browser console. This (poorly named) variable is being added by a plugin using wp_localize_script(). When I check the page source, I see that the data is being output, but not until *after* the script tags in the head. I think WP usually outputs the data immediately above the script tag for the file that uses it, but in this case it isn’t (a caching plugin is being used which is probably the cause). I notice that none of the other scripts seem to be giving undefined variable notices though, likely because they don’t attempt to use their localized data until the DOM is ready . So I’m wondering who is doing it wrong. Is it wrong for the plugin to assume that the variable will be there when the file is parsed, or should the caching plugin (or whatever the culprit) not be moving the localized data below the scripts?
>  



More information about the wp-hackers mailing list