[wp-hackers] wp_localize_script() issue

Philip Walton philip at philipwalton.com
Mon Jun 20 07:25:15 UTC 2011


Put another way, does anyone see a problem if PHP data is passed as JS 
with string keys like:

var jsVar = {
     "string-key": "Foo Bar"
};

Instead of:

var jsVar = {
     nonStringKey: "Foo Bar"
};

I'm not an expert of the subject, but this seems to work just fine for 
me. Does anyone see any reason not to change it to this?

This would be a really simple patch on line 63 of 
"includes/class.wp-scripts.php"


On 6/19/11 6:59 PM, Philip Walton wrote:
> When using wp_localize_script(), the third parameter accepts an array 
> of data to pass.
>
> I've just noticed that if the keys of the array contain minuses 
> (dashes) in them, this will cause errors in your javascript. Firebug 
> is telling me "missing : after property id", which I assume is a parse 
> error.
>
> I know I can solve this by not putting minuses in my array key names, 
> but for backwards comparability, it would make my life much easier if 
> I could in this situation.
>
> Has anyone else encountered this? Is there a known workaround other 
> than removing the minuses from the array keys or json_encoding the 
> entire array?


More information about the wp-hackers mailing list