Wp_reDirect()在WordPress中插入PHP插件时不起作用

时间:2018-10-08 作者:Grenston George

这是我在插入PHP插件中的代码。我在WordPress的一个页面中添加了这个插件生成的PHP代码段。这个wp_redirect 无法使用此代码段。

// define variables and set to empty values
$emailErr = $passwordErr = "";
$email = $password = "";

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}



if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } 
  else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

  if (empty($_POST["password"])) {
    $passwordErr = "Password is required";
  } else {
    $password = test_input($_POST["password"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
    //if (!preg_match("/\\b(?:(?:https?|ftp):\\/\\/|www\\.)[-a-z0-9+&@#\\/%?=~_|!:,.;]*[-a-z0-9+&@#\\/%=~_|]/i",$website)) {
    //  $websiteErr = "Invalid URL"; 
    //}


  if ($emailErr == "" && $passwordErr == "")
  {
  wp_redirect("https://grenstonhrs.000webhostapp.com",301);
  exit;
  }
  }

}

  ?>
  <div>
  <form id="form" method="POST">
Email
<br/>
<input name="email" style="width: 300px;" type="text" value="<?php echo $email;?>" />&nbsp;&nbsp;
<span style="color: #FF0000"><?php echo $emailErr;?></span>
<br/>
Password
<br/>
<input name="password" style="width: 300px;" type="password" />&nbsp;&nbsp;
<span style="color: #FF0000"><?php echo $passwordErr;?></span>
<br/>
<br/>
<input id="loginButton" name="loginButton" type="submit" />
</form>&nbsp;
<br/>
<a href="https://grenstonhrs.000webhostapp.com/sign-up/">Sign Up</a>
</div>

1 个回复
SO网友:Marc

我以前使用过WordPress的自定义帖子处理程序功能来解决与您遇到的问题类似的问题。以下是codex关于这方面的文件:https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)

基本上,您将使用一个操作来处理post请求:

function my_handle_form_submit() {

    // This is where you will control your form after it is submitted, you have access to $_POST here.

}

// Use your hidden "action" field value when adding the actions
add_action( \'admin_post_nopriv_my_simple_form\', \'my_handle_form_submit\' );
add_action( \'admin_post_my_simple_form\', \'my_handle_form_submit\' );
并修改表单a,以便WordPress知道如何使用您的函数来处理请求:

<!-- Submit the form to admin-post.php -->
<form action="<?php echo esc_url( admin_url(\'admin-post.php\') ); ?>" method="POST">

    <!-- Your form fields go here -->

    <!-- Add a hidden form field with the name "action" and a unique value that you can use to handle the form submission  -->
    <input type="hidden" name="action" value="my_simple_form">

</form>

结束

相关推荐

从PHP文件插入WordPress POST

我有一个网站,它包含很多新闻,我必须在一个PHP文件中列出这些新闻,现在我想在wordPress中插入每一条新闻作为帖子。。实现这一点的最佳实践方法是什么(新闻有100多篇文章),如何手动构建XML文件,以便在WordPress中导入?或者什么是post insert函数,所以我可以在PHP文件中调用它。。?新闻如下:https://www.steuerlex24.de/steuerkanzlei-koerfer/information_steuer-news有一个API可以将新闻导出到XML文件中。非常