wp_json_file_decode

函数
wp_json_file_decode ( $filename, $options = array() )
参数
  • (string) $filename Path to the JSON file.
    Required:
  • (array) $options { Optional. Options to be used with `json_decode()`. @type bool $associative Optional. When `true`, JSON objects will be returned as associative arrays. When `false`, JSON objects will be returned as objects. }
    Required:
    Default: array()
返回值
  • (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.
定义位置
相关方法
wp_json_encodewp_is_file_mod_allowedwp_nonce_fieldwp_filter_commentwp_handle_sideload
引入
5.9.0
弃用
-

wp_json_file_decode: 这个函数用于解码一个JSON编码的文件。它接受一个文件路径作为参数并返回一个解码的PHP变量。

读取和解码一个JSON文件。

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

常见问题

FAQs
查看更多 >