Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress: form redirect after submit

How can I implement a redirect after a custom form submission with PHP in WordPress?

<?php
if(isset($_POST['send'])) {     
  $email = trim($_POST['email']);
  if(empty($email)) {
    echo '<div class="error">Please enter your email.</div>';
  } else {
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      echo '<div class="error">Email is not valid.</div>';
    } else {        
      //redirect
      ?><script>window.location = "<?php echo home_url('/thank-you/');?>"</script><?php
    }
  }
}
?>
<form method="post" role="form">
  <div class="form-group">
    <label for="email">Email</label>
    <input type="text" class="form-control" id="email" name="email" value="<?php  if(!empty($email)) echo $email; ?>" placeholder="Enter your email..." />
  </div>
  <button type="submit" name="send" class="btn btn-default">Send</button>
</form>

This is a simple implementation of a custom form which I want to redirect to another site after submit. It works already with javascript, but maybe exists a better way to implement this redirect with PHP?

Any help would be appreciated. Thanks!

like image 525
Fox Avatar asked Jan 18 '26 14:01

Fox


1 Answers

try this

<?php wp_redirect( home_url('/thank-you/') ); exit; ?>
like image 196
Ram Sharma Avatar answered Jan 21 '26 06:01

Ram Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!