Pass Variable from PHP to Javascript

wp_enqueue_script('my-script','/path/to/whatever.js',…);

$params = array(
‘foo’ => ‘bar’,
‘setting’ => 123,
);

wp_localize_script( 'my-script''MyScriptParams'$params );

Output Example:

<script type=’text/javascript’>
/* <![CDATA[ */
var data = “[{\”title\”:\”Demo Title 1\”,\”content\”:\”Demo Content 1\”},{\”title\”:\”Demo Title 2\”,\”content\”:\”Demo Content 2\”}]”;
/* ]]> */
</script>

One thought on “Pass Variable from PHP to Javascript

  1. We absolutely love your blog and find a lot of your post’s to be what precisely I’m looking for.

    can you offer guest writers to write content for you?
    I wouldn’t mind composing a post or elaborating on a few of the subjects you write with regards to here. Again, awesome website!

Comments are closed.