Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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

wp_safe_redirect('http://example.com');
header("Content-Length: 0");
header("Connection: close");
flush();
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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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