rest_pre_serve_request хук-фильтрWP 4.4.0

Filters whether the REST API request has already been served.

Allow sending the request manually - by returning true, the API result will not be sent to the client.

Использование

add_filter( 'rest_pre_serve_request', 'wp_kama_rest_pre_serve_request_filter', 10, 4 );

/**
 * Function for `rest_pre_serve_request` filter-hook.
 * 
 * @param bool             $served  Whether the request has already been served.
 * @param WP_HTTP_Response $result  Result to send to the client. Usually a `WP_REST_Response`.
 * @param WP_REST_Request  $request Request used to generate the response.
 * @param WP_REST_Server   $server  Server instance.
 *
 * @return bool
 */
function wp_kama_rest_pre_serve_request_filter( $served, $result, $request, $server ){

	// filter...
	return $served;
}
$served(true|false)
Whether the request has already been served.
По умолчанию: false
$result(WP_HTTP_Response)
Result to send to the client. Usually a WP_REST_Response.
$request(WP_REST_Request)
Request used to generate the response.
$server(WP_REST_Server)
Server instance.

Список изменений

С версии 4.4.0 Введена.

Где вызывается хук

WP_REST_Server::serve_request()
rest_pre_serve_request
wp-includes/rest-api/class-wp-rest-server.php 466
$served = apply_filters( 'rest_pre_serve_request', false, $result, $request, $this );

Где используется хук в WordPress

wp-includes/default-filters.php 636
add_filter( 'rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4 );
wp-includes/rest-api.php 207
add_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );