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

actions - wp_redirect() not working on form submission with init hook

programmeradmin2浏览0评论

I am trying to redirect the user after form submission using wp_redirect() but it it is not working.

I am submitting data using init action hook. here is the code.

function ab_process_application_form()
{

    if (isset($_POST['new_application']) && isset($_POST['ab_application_nonce'])) {

        if (wp_verify_nonce($_POST['ab_application_nonce'], 'ab_application_form_nonce')) {

            // all $_POST and validation code

            ...

            // add record to database
            $insert_id = $db->insert($data, $format);

            // trigger action after form submit
            do_action('ab_application_submitted', $insert_id, $firstname, $lastname, $post_campaign);

            // redirect after form submitted
            wp_redirect(home_url('/application/thank-you'));

        } else {
            echo 'Not Verified';
        } // end nonce verification

    } // end check
} // end of function


// submit record on init hook
add_action('init', 'ab_process_application_form');

I am trying to redirect the user after form submission using wp_redirect() but it it is not working.

I am submitting data using init action hook. here is the code.

function ab_process_application_form()
{

    if (isset($_POST['new_application']) && isset($_POST['ab_application_nonce'])) {

        if (wp_verify_nonce($_POST['ab_application_nonce'], 'ab_application_form_nonce')) {

            // all $_POST and validation code

            ...

            // add record to database
            $insert_id = $db->insert($data, $format);

            // trigger action after form submit
            do_action('ab_application_submitted', $insert_id, $firstname, $lastname, $post_campaign);

            // redirect after form submitted
            wp_redirect(home_url('/application/thank-you'));

        } else {
            echo 'Not Verified';
        } // end nonce verification

    } // end check
} // end of function


// submit record on init hook
add_action('init', 'ab_process_application_form');
Share Improve this question asked Mar 2, 2017 at 5:51 pixelngrainpixelngrain 1,3901 gold badge23 silver badges50 bronze badges 3
  • 2 Try exiting after the wp_redirect call – bynicolas Commented Mar 2, 2017 at 5:59
  • @bynicolas OMG, so simple fix. Just to understand, why exit() is required? – pixelngrain Commented Mar 2, 2017 at 6:05
  • 1 It's in the docs, because the function does not exit so you need to do it after your call. I believe it's related to the PHP header() function that needs to exit after it's been called to make sure the code below doesn't get executed. – bynicolas Commented Mar 2, 2017 at 7:03
Add a comment  | 

1 Answer 1

Reset to default 2

The problem with your code is pretty simple. You don't terminate the script execution after doing redirect. So the header will be set, but browser will ignore it.

If you'll take a look at WP Code Reference, there is clearly stated:

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

So all you have to do is change your code like so:

function ab_process_application_form()
{
    if (isset($_POST['new_application']) && isset($_POST['ab_application_nonce'])) {

        if (wp_verify_nonce($_POST['ab_application_nonce'], 'ab_application_form_nonce')) {

            // all $_POST and validation code

            ...

            // add record to database
            $insert_id = $db->insert($data, $format);

            // trigger action after form submit
            do_action('ab_application_submitted', $insert_id, $firstname, $lastname, $post_campaign);

            // redirect after form submitted
            wp_redirect(home_url('/application/thank-you'));
            exit; // <-- this is the only change you need to do

        } else {
            echo 'Not Verified';
        } // end nonce verification

    } // end check
} // end of function


// submit record on init hook
add_action('init', 'ab_process_application_form');

PS. Almost always it will be good idea to add trailing slash to the URL you're redirecting to. Otherwise WP will perform one more redirect to add this slash.

PPS. Also, it would be much nicer, if you'd use admin_post hook instead of init to process POST requests.

发布评论

评论列表(0)

  1. 暂无评论