Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
wordpress arbitrary js from php using wp_localize_script
Working in 4.0
functions.php:
// localize_script js object hack.
wp_register_script('gimme-objects', get_stylesheet_directory_uri() . '/gimme-objects.js' );
wp_localize_script('gimme-objects', 'foo', array( 'l10n_print_after' => 'foo = {}' ));
wp_localize_script('gimme-objects', 'foo.bar', array("a" => 1));
wp_enqueue_script('gimme-objects', get_stylesheet_directory_uri() . '/gimme-objects.js' );
generated code:
/* <![CDATA[ */
var foo = [];
foo = {};
var foo.bar = {"a":"1"};
/* ]]> */
<script type='text/javascript' src='http://local.dev/wp-content/themes/objects-for-everyone/gimme-objects.js?ver=4.0'></script>
You can’t perform that action at this time.