• Resolved physicsinception

    (@physicsinception)


    Hi,

    I have a file called settings.html that include two files in the footer which are a javascript file and a json file.

    <script src="script.js"></script>
    <script src="settings.json"></script>

    Basically, my javascript file needs the json file to work properly.

    In my plugin.php file I enqueued my javascript file as well as my json file like this.
    I know how to enqueue a javascript but not a json file so I tried something like that.

    function register() {
            wp_enqueue_script( 'json_settings', plugins_url('settings.json', __FILE__,true) );
            wp_enqueue_script( 'js_script', plugins_url('script.js',array(json_settings) ,__FILE__),true );
    }
    add_action( 'admin_enqueue_scripts', 'register' );

    The problem here is that my javascript file works but without the json file. My javascript file cannot reach the datas inside the json file.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Not sure it’s the whole problem, but your dependency parameter needs quotes.
    array('json_settings')

    Moderator bcworkz

    (@bcworkz)

    Joy is correct about quotes. It’s also not the whole problem. The dependency arguments and version arguments are improperly commingled with the plugin URLs. I think you want this:

    wp_enqueue_script('json_settings', plugins_url('settings.json', __FILE__), array(), true );
    wp_enqueue_script('js_script', plugins_url('script.js', __FILE__), array('json_settings'), true );
    Thread Starter physicsinception

    (@physicsinception)

    Actually, I just copied it wrong.
    My code is actually like you mentioned, but it doesn’t work.

    I can see that both files are loaded, but separately.

    Moderator bcworkz

    (@bcworkz)

    Your JS cannot directly access a .json file loaded into the browser. The script either needs to directly read the file off the server, or you should convert your .json file into a .js file with a var assignment of the JSON. For example:
    settings.js

    var mySettings = [
      "key1": "value1",
      "key2": "foobar",
      "key3": "snafu",
      "an_integer": 1234
    ];
    Thread Starter physicsinception

    (@physicsinception)

    Sorry for the delay.
    It works, thanks !

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to enqueue or include json file to a plugin’ is closed to new replies.