I have a JSON file stored in my child theme directory: file.json). Separately, the PHP template for my page contains some JavaScript code (in the form of a <script>
tag). In that JS code, I want to write the contents of the JSON file to a variable. What is the proper approach to this?
- Can/should I enqueue the JSON file, just as I would a normal JS file (i.e.
wp_enqueue_scripts()
)? If so, how would I in-turn write the contents of the file to a JS variable? Would I do something likemyJson = json.parse('http://example.com/wp-contents/themes/your-theme/file.json'
)? - Can I just use
include
to include the JSON file on the page? Actually now that I think about it, one can only useinclude
with certain file extensions--correct? - Should I perhaps use PHP to save the contents of the JSON file to a PHP variable, then pass that variable to the JS code?
Thanks in advance.