1

This is probably a PHP question, but since I encountered in on a wordpress documentation page, maybe I will get better answers here.

On https://developer.wordpress.org/reference/functions/wp_redirect/ it says

Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;

I don't quite understand what will really happen here if exit is omitted here.

PHP's doc says exit is to "terminate the current script". So I assume if wp_redirect is called in function.php, the exit following it would stop the rest part of function.php from running, am I right? But why would I want that?

1 Answer 1

2

When you call wp_redirect it sends redirect HTTP headers, and that's all the browser needs to redirect, there is no reason to continue executing code. So you must exit. wp_redirect will not do it for you, and PHP won't do it for you if you send the http header to redirect.

If you do not exit, WordPress will continue to run the rest of the page generation sequence, loading all the code, which may confuse the browser, or lead to generating HTML the browser has no interest in, as well as wasting server resources.

After all why would you continue loading the page if the server has told you to go to a new location?

But more importantly, why would PHP or WordPress do the exiting for you? What if you needed to do cleanup? Or log that you had redirected the user? Exiting is your responsibility, WordPress and PHP won't do it for you, because that would be problematic.

6
  • Thanks. I've thought when I do wp_redirect a.k.a telling the server to send new http header to my browser, then the rest of the code won't get executed at all, hence adding an exit is unnecessary. What did I misunderstand?
    – shenkwen
    Dec 21, 2021 at 22:23
  • there is nothing to tell the server to stop executing the code, that's your responsibility, you have to call exit yourself, like all the docs say, it is absolutely necessary
    – Tom J Nowell
    Dec 22, 2021 at 2:39
  • @shenkwen does this answer your question? If so can you mark it as the accepted answer, if not can you clarify why?
    – Tom J Nowell
    Feb 11, 2022 at 20:37
  • Ok but why this work is not done in the wp_redirect function ? Are there any case of having to do things after the redirect, but before the exit ?
    – Gfra54
    Oct 25, 2023 at 8:43
  • sometimes it's useful to tell the browser to redirect then do some work instead of making it wait, that could be because you're implementing authentication and need to do additional steps such as closing connections, writing to audit logs, etc. Maybe you want to send additional headers e.g. for debugging purposes, or to log to a file that a redirect occurred because you're diagnosing a redirects problem. Maybe the user has uploaded a file and you redirect them onwards then continue processing the file in the background? If the redirect function exit'd none of that would be possible
    – Tom J Nowell
    Oct 25, 2023 at 9:57

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.