最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Why wp_die() doesn't work with wp_redirect but exit() works

programmeradmin3浏览0评论

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().

Share Improve this question asked Nov 19, 2020 at 14:39 Raashid DinRaashid Din 2182 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Depending 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.

发布评论

评论列表(0)

  1. 暂无评论