[wp-trac] [WordPress Trac] #41358: Shutdown hooks can significantly slow down REST API responses
WordPress Trac
noreply at wordpress.org
Mon Feb 10 22:00:38 UTC 2025
#41358: Shutdown hooks can significantly slow down REST API responses
-------------------------------------------------+-------------------------
Reporter: mikejolley | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting
| Review
Component: REST API | Version:
Severity: normal | Resolution:
Keywords: has-patch dev-feedback needs- | Focuses: rest-api,
testing | performance
-------------------------------------------------+-------------------------
Comment (by xipasduarte):
Just stumbled on this while looking to perform some operation after the
response is sent. The solution we employ is basically the same as the
proposed code, but we've put it so we could minimize the risk, which might
be helpful for the discussion.
Essentially we add a callback on the `shutdown`, but we put it with
priority `PHP_MAX_INT`, where we execute the response flush and add a
custom hook for things to run after the response is sent.
This allows us to maintain the most compatibility with plugins or themes,
while performing operations after a response is sent.
{{{#!php
<?php
add_action( 'shutdown', 'finish_request', PHP_MAX_INT );
function finish_request() {
// Something like the code proposed to flush the response.
// This hook will allows for a better interface.
do_action( 'shutdown_after_response' );
}
add_action( 'shutdown_after_response', 'after_response_operation' );
function after_response_operation() {
// Do an action the
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/41358#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list