On this page
wp_json_file_decode( string $filename, array $options = array() ): mixed
Reads and decodes a JSON file.
Parameters
$filenamestring Required-
Path to the JSON file.
$optionsarray Optional-
Options to be used with
json_decode().
associativeboolOptional. Whentrue, JSON objects will be returned as associative arrays.
Whenfalse, JSON objects will be returned as objects.
Default:
array()
Return
mixed Returns the value encoded in JSON in appropriate PHP type.
null is returned if the file is not found, or its content can't be decoded.
Source
File: wp-includes/functions.php. View all references
function wp_json_file_decode( $filename, $options = array() ) {
$result = null;
$filename = wp_normalize_path( realpath( $filename ) );
if ( ! $filename ) {
trigger_error(
sprintf(
/* translators: %s: Path to the JSON file. */
__( "File %s doesn't exist!" ),
$filename
)
);
return $result;
}
$options = wp_parse_args( $options, array( 'associative' => false ) );
$decoded_file = json_decode( file_get_contents( $filename ), $options['associative'] );
if ( JSON_ERROR_NONE !== json_last_error() ) {
trigger_error(
sprintf(
/* translators: 1: Path to the JSON file, 2: Error message. */
__( 'Error when decoding a JSON file at path %1$s: %2$s' ),
$filename,
json_last_error_msg()
)
);
return $result;
}
return $decoded_file;
}
Related
Uses
| Uses | Description |
|---|---|
| wp_normalize_path() wp-includes/functions.php | Normalizes a filesystem path. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
Used By
| Used By | Description |
|---|---|
| WP_Theme_JSON_Resolver::get_style_variations() wp-includes/class-wp-theme-json-resolver.php | Returns the style variations defined by the theme. |
| get_block_metadata_i18n_schema() wp-includes/blocks.php | Gets i18n schema for block’s metadata read from |
| WP_Theme_JSON_Resolver::read_json_file() wp-includes/class-wp-theme-json-resolver.php | Processes a file that adheres to the theme.json schema and returns an array with its contents, or a void array if none found. |
| WP_Theme_JSON_Resolver::translate() wp-includes/class-wp-theme-json-resolver.php | Given a theme.json structure modifies it in place to update certain values by its translated strings according to the language set by the user. |
| register_block_type_from_metadata() wp-includes/blocks.php | Registers a block type from the metadata stored in the |
Changelog
| Version | Description |
|---|---|
| 5.9.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_json_file_decode