Skip to content

Instantly share code, notes, and snippets.

@petenelson
Last active December 22, 2015 07:18
Show Gist options
  • Save petenelson/6436984 to your computer and use it in GitHub Desktop.
Save petenelson/6436984 to your computer and use it in GitHub Desktop.
WordPress: wp_localize_script with a stdClass
$test_data = array();
$test_data[] = new stdClass();
$test_data[0]->field1 = 'hello';
$test_data[0]->field2 = 'world';
$test_data[0]->my_object = new stdClass();
$test_data[0]->my_object->field3 = 'more hello world';
wp_localize_script( 'gmec-intranet', 'test_data', $test_data );
// outputs the following Javascript
// var test_data = [{"field1":"hello","field2":"world","my_object":{"field3":"more hello world"}}];
// console screenshot: http://i.imgur.com/lhyhOPw.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment