[wp-trac] [WordPress Trac] #25280: wp_localize_script unexpectedly convert numbers to strings

WordPress Trac noreply at wordpress.org
Wed Sep 11 21:55:33 UTC 2013


#25280: wp_localize_script unexpectedly convert numbers to strings
-----------------------------+------------------------------
 Reporter:  adamsilverstein  |       Owner:
     Type:  defect (bug)     |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  I18N             |     Version:  trunk
 Severity:  normal           |  Resolution:
 Keywords:  has-patch close  |
-----------------------------+------------------------------

Comment (by adamsilverstein):

 Replying to [comment:2 c3mdigital]:
 > Also to note the way JSON_NUMERIC_CHECK handles booleans.
 >
 > `wp_localize_script( 'testing', 'TESTING', array( 'false' => false,
 'true' => true, 'integer' => 234 ) );`
 >
 > Before patch:
 > `var TESTING = {"false":"","true":"1","integer":"234"};`
 >
 > after patch:
 > `var TESTING = {"false":"","true":1,"integer":234};`

 interesting points, thanks for the feedback! the boolean evaluation in JS
 won't change, right? "1" and 1 are both truthy.

 the patch does not rely on PHP 5.3; instead it improves the behavior of if
 5.3 is available. in 5.2 nothing changes.

 agree wp_localize_script is not intended  for this purpose, but in
 practice thats how its used- even internally by core. if we aren't going
 to try to fix the function, how about adding a new function for passing
 non-string values from php to js - providing a standard method is better
 than leaving it up to developers in my opinion.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/25280#comment:3>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list