How to get the path to current theme, but from a JS file?

You can use wp_localize_script to pass an object with the url to load before the file. and then in the file you can use it like testpage_obj.json_data

wp_register_script('testpage', get_stylesheet_directory_uri() . '/js/testscript.js', array('jquery'), '', true);
wp_localize_script( 'testpage', 'testpage_obj', array(
    'json_data' => get_stylesheet_directory_uri() . '/js/mydata.json'
) );
wp_enqueue_script('testpage');

In the JS file

jQuery.getJSON(testpage_obj.json_data)

Leave a Comment