How to include a JSON file on my page?
-
Hello. I have a JSON file stored in my child theme directory (e.g. file.json). Separately, the PHP template for my page (located in the same child theme directory) contains some JavaScript code (in the form of a
<script>
tag). In that JavaScript code, I want to write the contents of the JSON file to a variable. What is the proper approach to this?Should I enqueue the JSON file, just as I would a normal JS file (i.e. wp_enqueue_scripts)? If so, how would I write the contents of the file to a JS variable? Would I do something like
myJson = json.parse('file.json'
)?Or can I just use
include
to include the JSON file on the page?Or 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.
- The topic ‘How to include a JSON file on my page?’ is closed to new replies.