[wp-trac] [WordPress Trac] #25280: wp_localize_script unexpectedly converts numbers to strings
WordPress Trac
noreply at wordpress.org
Tue Aug 5 13:54:41 UTC 2014
#25280: wp_localize_script unexpectedly converts numbers to strings
----------------------------------------------+----------------------------
Reporter: adamsilverstein | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Future
Component: I18N | Release
Severity: normal | Version: 3.2
Keywords: has-patch dev-feedback 4.1-early | Resolution:
| Focuses: javascript
----------------------------------------------+----------------------------
Comment (by adamsilverstein):
Nice trick, thats a great suggestion for a workaround!
Replying to [comment:20 Fab1en]:
> I think this fix is good. Before this fix, you would get the same
behavior by wrapping your variables in another level of array (because the
variable will fail the `is_scalar` test) :
>
> {{{
> wp_enqueue_script( 'localize_test', get_template_directory_uri() .
'/localize_script.js', array(), '1.0.0', true );
> $array_to_localize = array(
> 'a_string' => __( 'Some String' ),
> 'a_number' => 10,
> 't_boolean' => true,
> 'f_boolean' => false,
> );
> wp_localize_script( 'main', 'localized_variable', array('fixed' =>
$array_to_localize) );
> }}}
>
> So the fix will add consistency.
>
> JavaScript provides [http://bonsaiden.github.io/JavaScript-
Garden/#types.casting type coercion] depending on the context, so plugin
breakage risks are really low. To break, a plugin would have to
> 1. Expect a number as a string, but declare it as a number in PHP
> 2. Use it in a context where it could be interpreted as a number without
any check. (for example, using `'The value is ' +
localized_variable.a_number` would automatically cast the variable to a
string).
--
Ticket URL: <https://core.trac.wordpress.org/ticket/25280#comment:22>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list