WordPress:表单提交后重定向


WordPress: form redirect after submit

如何在WordPress中使用PHP提交自定义表单后实现重定向?

<?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>

这是一个自定义表单的简单实现,我想在提交后重定向到另一个网站。它已经与javascript一起工作了,但也许有更好的方法可以用PHP实现这种重定向?

如有任何帮助,我们将不胜感激。谢谢

尝试这个

<?php wp_redirect( home_url('/thank-you/') ); exit; ?>

您也可以考虑这两种解决方案

第一个在functions.php中为java脚本重定向创建函数并在需要时调用的解决方案

function redirect($url){
    $string = '<script type="text/javascript">';
    $string .= 'window.location = "' . $url . '"';
    $string .= '</script>';
    echo $string;
}

第二个解决方案是让你的wordpress index.php看起来像这个

ob_start();
/*content of your index.php here*/
ob_flush();