I have a question related to the behaviour of WordPress.
I coded a function which on-call redirects the user to a page inside WordPress. I used it with add_query_arg()
.
Here is the function I created.
function wg_uep_license_fail_redirect( $message ) {
update_option( 'wg_uep_license_status', 'invalid' );
if ( ! empty( $message ) ) {
$redirectUrl = admin_url( 'admin.php?page=wp_uep');
$finalizedUrl = add_query_arg( array(
'license_activation' => 'false',
'message' => urlencode( $message ),
), $redirectUrl );
wp_redirect( $finalizedUrl );
// wp_die();
exit;
}
}
As you can see the function has a message parameter. Now if I call the function like this.
$message = 'hello dead';
wg_uep_license_fail_redirect( $message );
Everything works fine if I use exit
but if I uncomment wp_die()
and remove the exit
. Nothing seems to work.
Now my question is simple what is the difference. How WordPress handles the wp_die
or exit
?
NOTE: die()
also works but not wp_die()
.
I have a question related to the behaviour of WordPress.
I coded a function which on-call redirects the user to a page inside WordPress. I used it with add_query_arg()
.
Here is the function I created.
function wg_uep_license_fail_redirect( $message ) {
update_option( 'wg_uep_license_status', 'invalid' );
if ( ! empty( $message ) ) {
$redirectUrl = admin_url( 'admin.php?page=wp_uep');
$finalizedUrl = add_query_arg( array(
'license_activation' => 'false',
'message' => urlencode( $message ),
), $redirectUrl );
wp_redirect( $finalizedUrl );
// wp_die();
exit;
}
}
As you can see the function has a message parameter. Now if I call the function like this.
$message = 'hello dead';
wg_uep_license_fail_redirect( $message );
Everything works fine if I use exit
but if I uncomment wp_die()
and remove the exit
. Nothing seems to work.
Now my question is simple what is the difference. How WordPress handles the wp_die
or exit
?
NOTE: die()
also works but not wp_die()
.
1 Answer
Reset to default 2Depending on your context, wp_die()
will behave differently. This means if you're inside an AJAX request it will do something else than say a standard http request.
So depending on from where you're actually calling it, this can make a difference. Say you're in a normal http request, then it will call _default_wp_die_handler()
which besides adding headers also outputs HTML - which is not what you want.
Just stick with exit
(or die
), this is also recommended in the docs.