0

I have a custom form which posts data to wpdb.

Currently, after form submit, my form keeps the user on the same page, but I'd like to redirect them to a thank you page.

Here is how the form is set up currently:

<form action="<?php echo esc_url( get_the_permalink() ); ?>?ref=<?php echo $reference; ?>" method="post">
// fields
</form>

Note: $reference is getting a query string from the URL.

I am then getting the data and posting it to wpdb:

if(isset($_POST['submit'])){
  insert_row();
}

function insert_row(){

  global $wpdb;

  $rsvp_table = $wpdb->prefix . 'rsvp';

  // get and sanitize fields
  $fullname = sanitize_text_field( $_POST['full_name']);

  if ( ! wp_verify_nonce( $_POST['rsvp_nonce'], 'rsvp_nonce' ) ) {
    return; // Nonce verification failed, do not insert data
  } else {
    $wpdb->insert($rsvp_table,
      array(
        'fullname' => $fullname,
      ),
      array(
        '%s', // fullname
      )
    );
  }

  if ( $wpdb->last_error !== '' ) {
    echo $wpdb->last_error;  // There was an error
  } else {
    echo 'Data was inserted successfully';  // Success
  }

}

Then, in functions.php, I have the following:

add_action('init', 'redirectAfterSubmit');

function redirectAfterSubmit() {
  if (isset($_POST["submit"])) {
    wp_redirect( "/thank-you", 301 );
    die();
    }
  }
}

Now, on form submit, this does redirect me to the thank you page, but it doesn't post the data to the database. The redirect is happening before posting the data it seems?

Edit

My form markup and database insertion (inc form handling) is in a component called rsvp.php which is included on a custom template called homepage:

<?php

/* Template Name: Homepage */

get_header();
get_template_part('templates/components/rsvp');
get_footer();

?>

Edit 2

Handle insertion of row before redirect in functions.php

add_action('init', 'redirectAfterSubmit');

function redirectAfterSubmit() {
  if (isset($_POST["submit"])) {
    insert_row();
    wp_redirect( "/thank-you", 301 );
    die();
  }
}

function insert_row(){
  // form handing here
}
8
  • where and when is your form handling code running? I notice that you didn't share where the form handling code was located or any information about when it runs. Can you edit your question to include this missing information? Is that code running in a page template? A shortcode? Hook?
    – Tom J Nowell
    Dec 7, 2022 at 15:23
  • Hi @TomJNowell - Done. Have added the info the the question.
    – Freddy
    Dec 7, 2022 at 15:32
  • ah, you can't redirect after your form as long as the handling is inside a theme template. Redirects need to happen before any output occurs so that the right HTTP headers are sent, which also means your form handling needs to happen before then too
    – Tom J Nowell
    Dec 7, 2022 at 15:50
  • @TomJNowell - Ah, I see and makes sense. I've updated my question to showcase code snippet on what I think you're referring to (which looks to be working for me). May come in handy for others :)
    – Freddy
    Dec 7, 2022 at 16:58
  • Thanks for the pointers!
    – Freddy
    Dec 7, 2022 at 16:58

1 Answer 1

0

Form handling needs to happen in functions.php (or equivalent) so that it triggers before headers are sent. Here is the how I achieved redirection after form submit:

add_action('init', 'redirectAfterSubmit');

function redirectAfterSubmit() {
  if (isset($_POST["submit"])) {
    insert_row();
    wp_redirect( "/thank-you", 301 );
    die();
  }
}

function insert_row(){
  // form handing here
}
1
  • 1
    Just note that template_redirect is the conventional hook to use when redirecting. What you have will probably work, so it's not wrong, but template_redirect is the convention. Dec 9, 2022 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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