Passing Multi-Dimensional Arrays from WordPress to JavaScript

On its own wp_localize_script() can’t handle multi-dimensional arrays, but you can get around that by encoding the child arrays in JSON. That doesn’t handle HTML content very well, though, so another way to do it is to use the l10n_print_after parameter. That’s kind of ugly, but it seems like the best way available right now. It looks like WordPress 3.3 might have a wp_add_js_data() function, which would be much nicer.

Leave a Reply

Your email address will not be published. Required fields are marked *