1

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');
  • 1
    Try exiting after the wp_redirect call – bynicolas Mar 2 '17 at 5:59
  • @bynicolas OMG, so simple fix. Just to understand, why exit() is required? – pixelngrain Mar 2 '17 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 Mar 2 '17 at 7:03
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.