简体   繁体   English

WordPress:提交后表单重定向

[英]WordPress: form redirect after submit

How can I implement a redirect after a custom form submission with PHP in WordPress? 在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>

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? 它已经可以使用javascript了,但是也许存在更好的方法来用PHP实现此重定向?

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

尝试这个

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

You can also consider these 2 solutions as well 您也可以考虑这两种解决方案

first solution to create a function for java script redirect in functions.php and call when ever you want like this 第一个为java脚本重定向功能创建函数的解决方案,可以在functions.php中调用,并在需要时调用,就像这样

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

second solution is to make your wordpress index.php look like this 第二种解决方案是使您的wordpress index.php看起来像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM