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';


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


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


and the file is exactly there,

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

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

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.

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

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


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

// \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;
  • 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.