wp_redirect is not working sometimes

I've tested the following code in my function.php:

if ( $_GET['test'] ) {
  wp_redirect('https://google.com');
}

and it works, if I go to https://example.com/?test=true, I will be redirected to Google.

But I found out when I put wp_redirect() in a function hooked to some action, it won't work. Instead after its execution users will stay on the same page, and, in my case, some CSS files seems not being loaded.

Since I haven't figured out what kind of actions will trigger this behavior, it is hard for me to reproduce the problem and demonstrate it in a simple way. So I take some screen recording, hope that will help make sense of the problem I am experiencing: https://www.loom.com/share/4cfad90fde494fbd99db113c30b7555b

Topic wp-redirect Wordpress

Category Web


As the docs say:

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

wp_redirect( $url );
exit;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.