0

I'm having the following problem with a Wordpress installation on a VPS server. I'm trying to read from a JSON file but the returned address from get_template_directory() is wrong:

$url = get_template_directory() . '/inc/includes/acf-fonticonpicker';

$json_file = trailingslashit($url) . 'icons/selection.json';

if($wp_filesystem->exists($json_file)){

   $json_content = $wp_filesystem->get_contents($json_file);

}

$json_file is empty because the JSON file is not found. The returned address is:

/var/www/domainname/data/www/domain/wp-content/themes/couponhut/inc/includes/acf-fonticonpicker/icons/selection.json

and the file is exactly there,

What I'm doing wrong? Any help will be greatly appreciated!

1
  • you could just get the json by file_get_contents().
    – fischi
    Jan 19 '16 at 14:12
1

You can read data on your server with file_get_contents(). If you want to make sure the file exists and is readable then use is_readable(). You don't really need to use trailingslashit() in this case because you're constructing the URI yourself.

// path to file under current theme 
$json_file = get_template_directory() . '/inc/includes/acf-fonticonpicker/icons/selection.json';

// make sure the file exists and is readable
if ( is_readable( $json_file ) ) {

    // pull the data but don't give an error if there is a problem
    if ( ! empty ( $json_content = @file_get_contents( $json_file ) ) ) {

        // convert to an array
        $json_array = json_decode( $json_content, true );
    } else {
        // show error message here
    }
}

Unfortunately file_get_contents() might be flagged in Theme Check.

File systems are funny, if you know it exists then it's possible that / and \ need to be swapped in the path on occasion.

// BEFORE
// /vagrant/site/wp-content/themes/twentysixteen/inc/includes/acf-fonticonpicker/icons/selection.json

$json_file = str_replace('/', '\\', $json_file);

or

$json_file = str_replace('/', '\\\\', $json_file);

// AFTER
// \vagrant\site\wp-content\themes\twentysixteen\inc\includes\acf-fonticonpicker\icons\selection.json

Another way is to access via the url.

// url of file
$json_file = get_template_directory_uri() . '/inc/includes/acf-fonticonpicker/icons/selection.json';

// request the file
$response  = wp_remote_get( $json_file );

try {
    // Note that we decode the body's response since it's the actual JSON feed
    $json = json_decode( $response[ 'body' ], true );
} catch( Exception $ex ) {
    $json = NULL;
}  
3
  • Thanks for the suggestion. I tried with wp_remote_fopen and it worked but is it the correct way to handle such situations?
    – Ziik
    Jan 19 '16 at 18:20
  • @No, only reading from it.
    – Ziik
    Jan 19 '16 at 18:35
  • It just dawned on me. You might need to reverse the slash direction for local file system. Give that a try.
    – jgraup
    Jan 19 '16 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.