# rest_preload_api_request()

URL: https://chugunov.pro/api-wordpress/functions/rest_preload_api_request/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 5.0.0.

## Сигнатура

```php
rest_preload_api_request( array $memo, string $path ): array
```

## Описание

Предполагается вызов в контексте array_reduce.

## Параметры

- `$memo` `array` — обязательный. Аккумулятор reduce.
- `$path` `string` — обязательный. Путь REST API для предварительной загрузки.

## Возвращаемое значение

`array`

## Исходный код

Файл: `wp-includes/rest-api.php:2937`

```php
function rest_preload_api_request( $memo, $path ) {
	/*
	 * array_reduce() doesn't support passing an array in PHP 5.2,
	 * so we need to make sure we start with one.
	 */
	if ( ! is_array( $memo ) ) {
		$memo = array();
	}

	if ( empty( $path ) ) {
		return $memo;
	}

	$method = 'GET';
	if ( is_array( $path ) && 2 === count( $path ) ) {
		$method = end( $path );
		$path   = reset( $path );

		if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) {
			$method = 'GET';
		}
	}

	// Remove trailing slashes at the end of the REST API path (query part).
	$path = untrailingslashit( $path );
	if ( empty( $path ) ) {
		$path = '/';
	}

	$path_parts = parse_url( $path );
	if ( false === $path_parts ) {
		return $memo;
	}

	if ( isset( $path_parts['path'] ) && '/' !== $path_parts['path'] ) {
		// Remove trailing slashes from the "path" part of the REST API path.
		$path_parts['path'] = untrailingslashit( $path_parts['path'] );
		$path               = str_contains( $path, '?' ) ?
			$path_parts['path'] . '?' . ( $path_parts['query'] ?? '' ) :
			$path_parts['path'];
	}

	$request = new WP_REST_Request( $method, $path_parts['path'] );
	if ( ! empty( $path_parts['query'] ) ) {
		parse_str( $path_parts['query'], $query_params );
		$request->set_query_params( $query_params );
	}

	$response = rest_do_request( $request );
	if ( 200 === $response->status ) {
		$server = rest_get_server();
		/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
		$response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $server, $request );
		$embed    = $request->has_param( '_embed' ) ? rest_parse_embed_param( $request['_embed'] ) : false;
		$data     = (array) $server->response_to_data( $response, $embed );

		if ( 'OPTIONS' === $method ) {
			$memo[ $method ][ $path ] = array(
				'body'    => $data,
				'headers' => $response->headers,
			);
		} else {
			$memo[ $path ] = array(
				'body'    => $data,
				'headers' => $response->headers,
			);
		}
	}

	return $memo;
}
```

## История изменений

- 5.0.0 — Introduced.

## Связанные

Использует: [`rest_parse_embed_param`](https://chugunov.pro/api-wordpress/functions/rest_parse_embed_param/), [`rest_get_server`](https://chugunov.pro/api-wordpress/functions/rest_get_server/), [`rest_do_request`](https://chugunov.pro/api-wordpress/functions/rest_do_request/), `WP_REST_Request::__construct`, [`untrailingslashit`](https://chugunov.pro/api-wordpress/functions/untrailingslashit/), [`rest_ensure_response`](https://chugunov.pro/api-wordpress/functions/rest_ensure_response/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/rest_preload_api_request/
