Filter wp_redirect() to stop redirect under certain condition

I am developing a tool to print PHP debug messages on screen.

I would like to stop the wp_redirect() function from actually doing the redirect if the debug tool contains a message that it wants to display.

One way would be to check this before calling wp_redirect() at all, but it’s preferable to have something like a before_redirect filter where I can decide to “go on” or “don’t go”.

I have seen the wp_redirect filter allows me to filter the redirect location.

I could say “go back to the same place” but that would be a new http request and the logs would be already lost.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As you can see from the source code, wp_redirect always ends in a PHP header request, except if the location is empty, because then it doesn’t know where to redirect. So you can indeed use the wp_redirect filter. If you return an empty string under certain circumstances, the redirect will not happen.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x