rest_pre_dispatch
云策文档标注
概述
rest_pre_dispatch 是一个 WordPress REST API 的过滤器,允许在请求分发前拦截并修改响应。开发者可以通过此 Hook 返回非空值来接管请求处理。
关键要点
- 用于在 REST API 请求分发前过滤预计算结果,可返回自定义响应或 null 以保持原流程
- 参数包括 $result(可替换响应的值)、$server(WP_REST_Server 实例)和 $request(WP_REST_Request 对象)
- 在 WP_REST_Server::dispatch() 等核心方法中调用,自 WordPress 4.4.0 版本引入
代码示例
$result = apply_filters( 'rest_pre_dispatch', null, $this, $request );
原文内容
Filters the pre-calculated result of a REST API dispatch request.
Description
Allow hijacking the request before dispatching by returning a non-empty. The returned value will be used to serve the request instead.
Parameters
$resultmixed-
Response to replace the requested version with. Can be anything a normal endpoint can return, or null to not hijack the request.
$serverWP_REST_Server-
Server instance.
$requestWP_REST_Request-
Request used to generate the response.
Source
$result = apply_filters( 'rest_pre_dispatch', null, $this, $request );
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |