0

This is a sample hook that is run after a particular form on a Wordpress admin page is submitted, which I found in this StackOverflow answer:

public function foobar_save_admin_action()
{
    // Do your stuff here
    wp_redirect( $_SERVER['HTTP_REFERER'] );
    exit();
}

Is the exit() required after wp_redirect()? What would happen if I didn't write it?

This answer says that you should write exit or die after performing the redirect but it doesn't explain why.

Zenul_Abidin
  • 573
  • 8
  • 23
  • 2
    Yes, it's required as shared in the [official wordpress documentation](https://developer.wordpress.org/reference/functions/wp_redirect/). It's required because all `wp_redirect` does is the equivalent of a `header("Location: url")` which requires an exit to stop further headers or content to be output after the redirect – Andrea Olivato Mar 17 '22 at 07:06
  • Here's a more in-depth explanation: https://stackoverflow.com/questions/2747791/why-i-have-to-call-exit-after-redirection-through-headerlocation-in-php – Andrea Olivato Mar 17 '22 at 07:07

1 Answers1

0

If you don't have any code under header, you don't have to.

If you have code you definitely should. Otherwise the script execution is not terminated. Setting another header alone is not enough to redirect.

Abdullah Bozdağ
  • 136
  • 3
  • 13