I'm calling a custom function when someone submits comments using the following to plug into the comment_post action: add_action('comment_post', 'MY_FUNCTION');

The problem is that my function is extremely heavy, causing about a 5 second delay between the time when someone submits a comment to the time when the page refreshes and they view their comment. Is there any way that my custom function could run in the background, so that they could be redirected right away? Alternatively, would it be safe to redirect them directly at the top of my function, then run the rest of my function after they had already been redirected?

1 Answer 1


If you're sending a redirect, then you can also just close the connection early and continue processing.

header("Content-Length: 0");
header("Connection: close");
do_something(); // continue processing whatever, user is already redirecting by now

Reference: http://www.php.net/manual/en/features.connection-handling.php#104541

Read those user comments carefully, there's some caveats there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.