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("https://wordpress.stackexchange.com/", '\\', $json_file);
or
$json_file = str_replace("https://wordpress.stackexchange.com/", '\\\\', $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;
}