функция
since 5.9.0
wp_json_file_decode()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_json_file_decode( string $filename, array $options = array() ): mixed
Описание
Читает и декодирует JSON-файл.
Параметры
$filename
string
обязательный
Путь к JSON-файлу.
$options
array
необязательный
= array()
Опции, используемые с json_decode().
associative bool Необязательно. Если true, объекты JSON будут возвращены в виде ассоциативных массивов.
Если false, объекты JSON будут возвращены в виде объектов. Значение по умолчанию false.
Возвращаемое значение
mixed
null
Исходный код
wp-includes/functions.php:4676
function wp_json_file_decode( $filename, $options = array() ) {
$result = null;
$filename = wp_normalize_path( realpath( $filename ) );
if ( ! $filename ) {
wp_trigger_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_get_contents( $filename ), $options['associative'] );
if ( JSON_ERROR_NONE !== json_last_error() ) {
wp_trigger_error(
__FUNCTION__,
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;
}
История изменений
| Версия | Описание |
|---|---|
| 5.9.0 | Introduced. |

