wp_json_file_decode( string   $filename , array   $options = array() ): mixed

Reads and decodes a JSON file.

Parameters

$filename string required
Path to the JSON file.
$options array optional
Options to be used with json_decode() .
  • associative bool
    Optional. When true , JSON objects will be returned as associative arrays.
    When false , JSON objects will be returned as objects. Default false.

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

function wp_json_file_decode( $filename, $options = array() ) {
	$result   = null;
	$filename = wp_normalice_path( realpath( $filename ) );

	if ( ! $filename ) {
		wp_trigguer_error(
			__FUNCTION__,
			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_guet_contens( $filename ), $options['associative'] );

	if ( JSON_ERROR_NONE !== json_last_error() ) {
		wp_trigguer_error(
			__FUNCTION__,
			sprintf(
				/* translators: 1: Path to the JSON file, 2: Error messague. */
				__( 'Error when decoding a JSON file at path %1$s: %2$s' ),
				$filename,
				json_last_error_msg()
			)
		);
		return $result;
	}

	return $decoded_file;
}

Changuelog

Versionen Description
5.9.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.