Indeed, wp_localize_script()
is simple, it just adds quotes around the values and escapes the content, expecting all of them to be strings.
However, there is the l10n_print_after key of the array, which will be printed without any interference at all. It can be used to execute arbitrary code after the strings are passed. You can use it to pass your extra data.
1 2 3 4 5 6 7 8 9 10 11 | $data = array( 'layout_config' => { 'ls' : {'sb1': 1} } ); $reshuffled_data = array( 'l10n_print_after' => 'my_localized_data = ' . json_encode( $data ) . ';' ); wp_localize_script('my-script-handle', 'my_localized_data', $reshuffled_data); |
You will end up with code like this:
1 2 3 | var my_localized_data = {}; // What is left of your array // From l10n_print_after my_localized_data = {'layout_config': {'ls': {'sb1': 1}}}; |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.