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

functions - Postback redirect through add_action is not triggered

programmeradmin1浏览0评论

template:

<form action="<?php echo esc_url( admin_url('admin-post.php')); ?>" method="post">
    <input type="text" name="boo" id="boo" required>
    <input type="hidden" name="action" value="foo">
</form>

I have add the hook in theme functions.php and moo is registered:

add_action( 'admin_post_foo', 'moo' );
add_action( 'admin_post_nopriv_foo', 'moo' );
function moo() {
    wp_safe_redirect(
        esc_url(
            site_url( 'http://127.0.0.1:8000/?page_id=5' )
        )
    );
}

and still after post it hangs on admin_post.php

I don't have debug and I'm clueless of whether the hook is invoked so any idea of how to determine that as well?

template:

<form action="<?php echo esc_url( admin_url('admin-post.php')); ?>" method="post">
    <input type="text" name="boo" id="boo" required>
    <input type="hidden" name="action" value="foo">
</form>

I have add the hook in theme functions.php and moo is registered:

add_action( 'admin_post_foo', 'moo' );
add_action( 'admin_post_nopriv_foo', 'moo' );
function moo() {
    wp_safe_redirect(
        esc_url(
            site_url( 'http://127.0.0.1:8000/?page_id=5' )
        )
    );
}

and still after post it hangs on admin_post.php

I don't have debug and I'm clueless of whether the hook is invoked so any idea of how to determine that as well?

Share Improve this question asked Apr 25, 2019 at 12:27 Bat ManBat Man 1031 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

Let's say your site's address is example. When you want to redirect to example/page/subpage you should use site_url('/page/subpage').

I assume the WordPress address is http://127.0.0.1:8000. You have in your code
site_url( 'http://127.0.0.1:8000/?page_id=5' ) and it will be converted to this address: http://127.0.0.1:8000http://127.0.0.1:8000/?page_id=5.

Try using this redirection:

wp_safe_redirect( site_url( '/?page_id=5' ) );

Second issue, if you'll take a look at code reference, you will find there:

wp_safe_redirect() does not exit automatically and should almost always be followed by exit.

发布评论

评论列表(0)

  1. 暂无评论