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!
try this
<?php wp_redirect( home_url('/thank-you/') ); exit; ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With