admin管理员组文章数量:1134555
In WordPress, is there an action hook or anything which allows you to run a callback after the response to a custom REST API endpoint, registered via register_rest_route()
, has been sent to the client? Ideally with the data of the request payload?
The closest thing we could find so far is the rest_post_dispatch
hook, which however still happens before the response is sent to the client.
Context: The reason why we have to do this is to execute multiple API calls after a successful REST API call, whose completion is irrelevant for the response delivered to the client. Hence to avoid that the request processing time appears to be much longer than it needs to be, as these other API requests can take up to multiple seconds to be fully processed.
In WordPress, is there an action hook or anything which allows you to run a callback after the response to a custom REST API endpoint, registered via register_rest_route()
, has been sent to the client? Ideally with the data of the request payload?
The closest thing we could find so far is the rest_post_dispatch
hook, which however still happens before the response is sent to the client.
Context: The reason why we have to do this is to execute multiple API calls after a successful REST API call, whose completion is irrelevant for the response delivered to the client. Hence to avoid that the request processing time appears to be much longer than it needs to be, as these other API requests can take up to multiple seconds to be fully processed.
Share Improve this question asked May 5, 2023 at 22:31 DevelJoeDevelJoe 5276 silver badges21 bronze badges2 Answers
Reset to default 0You can look into queues e.g. create a custom database table, and write to it within the rest_post_dispatch
or rest_pre_serve_request
filter just before it is dispatched.
Then you can bulk process the queue (first record in is the first record out) via e.g. wp-cron
and the user will not be affected by that potentially heavy processing. A custom column for the processing status might be useful as well.
With a real cron (not wp-cron) you could better control the timing/frequency of the processing.
Hooks
I don't think the exact thing you're looking for exists. serve_request()
echos the output, and then returns to rest_api_loaded()
, which immediately die
s.
The only action that runs after the request is echo
'd to the output buffer is shutdown
. That's a callback for register_shutdown_function()
, so it runs after all the normal code paths have finished, but before the PHP process exits.
Even if you hooked into shutdown
, though, the request still hasn't necessarily been sent to the client (that may vary a bit depending on server config, though).
I think it's worth considering if you need to wait until it's sent. Are there relevant differences between that point and the point at which the response has been generated? Using rest_post_dispatch
-- or even rest_pre_echo_response
if you want to be as late as possible -- would give you access to the request and its response. The response isn't going to change, unless another plugin modifies it. If you're worried about that, you could hook in at a later priority to avoid that.
Batching
If your routes support it, you may want to look into sending batch requests. By default all requests are processed, even if some of them fail.
Yet another approach would be to create your own endpoint, which uses an internal request to do the batching.
本文标签: plugin developmentExecute callback after REST API request completesResponse is sent
版权声明:本文标题:plugin development - Execute callback after REST API request completes + Response is sent 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736835894a1954892.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论