wp_json_file_decode() WordPress Function

The wp_json_file_decode() function is used to decode a JSON file. This function is useful for reading data from a JSON file that is in a WordPress format.

wp_json_file_decode( string $filename, array $options = array() ) #

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 value: array()


Top ↑

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.


Top ↑

Source

File: wp-includes/functions.php

function wp_json_file_decode( $filename, $options = array() ) {
	$result   = null;
	$filename = wp_normalize_path( realpath( $filename ) );
	if ( ! file_exists( $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;
}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More