wp_redirect acting weird. requires die() afterward?
I am writing a wordpress plugin that redirects the user. for some reason, wp_redirect does not work as think it should, namely: it seems not to redirect the user at all, unless I put a die command 开发者_JAVA百科in directly after. here is some code:
switch($_GET['wp_favorites_tax'])
{
case 'post_tag':
wp_redirect(add_query_arg('tag', $_GET['wp_favorites_term'], get_bloginfo('url')));
die();
break;
case 'category':
wp_redirect(add_query_arg('cat', $_GET['wp_favorites_term'], get_bloginfo('url')));
die();
break;
default:
wp_redirect(get_bloginfo('url'));
die();
}
It seems really strange to me that I should have to tell my script to die so that the redirect can work. I have also tried a basic
header("Location: $location);
to similar ends, ie: it still requires the die() in order to work. really perplexing me. Thanks.
This is normal behaviour for header()
under certain circumstances, I don't remember the details why right now (I think it had to do with output buffering). I'm sure somebody will be able to shed some light on this.
Anyway, terminating the script's execution after a header("Location...")
is something one should get used to anyway - the sending of a header
is just a request to the browser to please move to a new location, but will not prevent the sending of the rest of the document, which can have catastrophic consequences when used in login systems (the user gets a header
redirect because they're not logged in, but also gets served all the possibly confidential data in the document body).
Always, always call die()
or exit
after doing a header redirect.
Usually, a HTTP redirect contains a message body for clients which don’t follow the header automatically. Try this in Opera (Options: Network).
Since wp_direct()
may be called before the translation is loaded, WordPress offers you the chance to add our own (localized) message body.
In short: I think, the current solution is the best if you care about I18n.
精彩评论