Menu

WordPress: export JS from php

May 30, 2017 - javascript, php, WordPress

Prepare the data in functions.php

// functions.php
    wp_localize_script(
        'main_js',                // name of script name
        'theData',            //  name of the object we want to output
        array('day' => 'Friday') // output
    );

Output on .php page

<![CDATA[ */
     var theData = {"day":"Friday"};
 ]]> */

Reference

wp_localize_script