钩子文档

rest_request_after_callbacks

💡 云策文档标注

概述

rest_request_after_callbacks 是一个 WordPress REST API 钩子,用于在执行所有回调后立即过滤响应。它允许插件执行清理操作,例如撤销在 rest_request_before_callbacks 中做的更改。

关键要点

  • 此钩子在执行任何 REST API 回调后立即触发,用于过滤响应。
  • 它不会在认证失败或路由未匹配的请求中调用。
  • 注意:endpoint 的 permission_callback 可能在此钩子之后被调用。
  • 参数包括 $response(响应对象)、$handler(路由处理器)和 $request(请求对象)。
  • 源代码示例:$response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request );

代码示例

$response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request );

注意事项

  • 此钩子不适用于认证失败或未匹配路由的请求。
  • permission_callback 可能在此钩子之后执行,需注意时序问题。

📄 原文内容

Filters the response immediately after executing any REST API callbacks.

Description

Allows plugins to perform any needed cleanup, for example, to undo changes made during the ‘rest_request_before_callbacks’ filter.

Note that this filter will not be called for requests that fail to authenticate or match to a registered route.

Note that an endpoint’s permission_callback can still be called after this filter – see rest_send_allow_header().

Parameters

$responseWP_REST_Response|WP_HTTP_Response|WP_Error|mixed
Result to send to the client.
Usually a WP_REST_Response or WP_Error.
$handlerarray
Route handler used for the request.
$requestWP_REST_Request
Request used to generate the response.

Source

$response = apply_filters( 'rest_request_after_callbacks', $response, $handler, $request );

Changelog

Version Description
4.7.0 Introduced.