wp_redirect not working after submitting form

You can only use wp_redirect before content is sent to the browser. If you were to enable php debugging you’d see a “headers already sent” error due to get_header() on the first line.

Rather than process the form in the template, you can hook an earlier action, like wp_loaded, and save some queries to the db if you’re just going to redirect away.

EDIT, example-

add_action( 'wp_loaded', 'wpa76991_process_form' );
function wpa76991_process_form(){
    if( isset( $_POST['my_form_widget'] ) ):
        // process form, and then
        wp_redirect( get_permalink( $pid ) );
        exit();
    endif;
}

Using an action, you can keep the code out of and separated from your templates. Combine this with a shortcode to output the form and wrap it all in a class to save state between processing/output, and you could do it all without touching the front end templates.

Leave a Comment

tech