钩子文档

rest_post_dispatch

💡 云策文档标注

概述

rest_post_dispatch 是一个 WordPress REST API 钩子,用于在响应返回给客户端之前进行修改。它允许开发者拦截和调整 WP_HTTP_Response 或 WP_REST_Response 对象。

关键要点

  • rest_post_dispatch 是一个过滤器钩子,应用于 REST API 响应。
  • 它接收三个参数:$result(WP_HTTP_Response 或 WP_REST_Response)、$server(WP_REST_Server 实例)和 $request(WP_REST_Request 对象)。
  • 钩子可用于修改响应数据、添加额外信息或处理错误。
  • 在 WordPress 4.4.0 中引入,4.5.0 版本扩展应用于嵌入式响应。

代码示例

$result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $request );

注意事项

  • 确保正确处理 $result 参数,它可能是 WP_HTTP_Response 或 WP_REST_Response 类型。
  • 使用此钩子时,注意避免破坏 REST API 的预期行为或引入安全漏洞。

📄 原文内容

Filters the REST API response.

Description

Allows modification of the response before returning.

Parameters

$resultWP_HTTP_Response
Result to send to the client. Usually a WP_REST_Response.
$serverWP_REST_Server
Server instance.
$requestWP_REST_Request
Request used to generate the response.

Source

$result = apply_filters( 'rest_post_dispatch', rest_ensure_response( $result ), $this, $request );

Changelog

Version Description
4.5.0 Applied to embedded responses.
4.4.0 Introduced.