Why “exit” is needed after wp_redirect?

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.

tech