• Resolved wpdsbarnes

    (@wpdsbarnes)


    Hi all,
    I’m not quite understanding how to read a JSON file from a PHP file.

    Here’s the setup:
    I am running an installation of WP on my localhost. This site will be moved via FTP to a hosting service in the near future (Ideally as soon as I figure this JSON thing out).

    I’ve created a plugin and have this file structure:

    – plugins/
    — dereks-extension-chord/
    — dereks-extension-chord.php
    — boss_pedals.json

    I simply want to store the JSON data from boss_pedals.json in a variable in the dereks-extension-chord.php

    I found a very similar question here. which suggested using: $json = file_get_contents('path-to-file.json').

    This seemed rather promising but did not work for me.

    Another find, a little more comprehensive can be seen here. No luck with that either.

    I began reading about the file system API, but I’m becoming convinced this isn’t what I’m looking for (I don’t need to switch users, for example).

    My current code is mostly comments from things that have not worked:

    function get_boss_pedals(){
        // $path = $_SERVER['DOCUMENT_ROOT'] . '/SEO-Grams/wp-content/plugins/dereks-extension-chord/boss_pedals.json';
        // $path = WP_PLUGIN_URL . 'dereks-extension-chord/boss_pedals.json';
        // $path = plugin_dir_url( __FILE__ ) . 'boss_pedals.json';
    
        // $dir  = trailingslashit( plugin_dir_url( __FILE__ ) );
        // $url  = wp_register_script( 'boss_pedals', $dir . 'boss_pedals.json' );
        // $req  = wp_remote_get( $url );
        // $body = wp_remote_retrieve_body( $req );
        // $data = json_decode( $body );
        // paragraph_wrapper returns '<p>' . $peram . '</p>'
        // return paragraph_wrapper(var_dump( $data ));
    }
    
    add_shortcode('test', 'get_boss_pedals');
    

    Apologies if my eyes have grown weary and I’ve missed the obvious here, but when I run file_exists() on any of the above $path vars above, I get false.

    Why?

    Thanks in advance.

    • This topic was modified 3 years, 2 months ago by wpdsbarnes.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Dion

    (@diondesigns)

    Try this:

    $json = file_get_contents(__DIR__ . '/boss_pedals.json');

    __DIR__ is a PHP global constant that contains the directory name of the script currently executing.

    Hi @wpdsbarnes! If I understand correctly, it sounds like you’re trying to serve a JSON file that’s located alongside the plugin itself. I created a similar plugin (albeit using plugin_dir_path rather than plugin_dir_url. Here’s my code for the plugin, I added the [testjson] shortcode to an example page and got it to load:


    function get_boss_pedals(){
    $path = plugin_dir_path(__FILE__) . 'sample_json.json';
    $data = file_get_contents($path);
    return "<p>" . $data . "</p>";
    }

    add_shortcode('testjson', 'get_boss_pedals');

    Edit screenshot: https://cloudup.com/ikebDsGIpGE
    Page screenshot: https://cloudup.com/iInOLaNlNdB

    Do you mind giving that a shot to see if it works?

    Thread Starter wpdsbarnes

    (@wpdsbarnes)

    @diondesigns & @aetherunbound

    you’re both awesome!
    I’d stopped putting the paths into file_get_contents() at some point (and apparently was checking if a string was a file).

    Thank you both so much for the response!

    Resolved.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trouble reading a JSON file in a PHP file.’ is closed to new replies.