最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin3PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far