1

This is my code in insert PHP plugin. I have added this PHP snippet generated by the plugin in a page in WordPress. The wp_redirect is not working with this snippet.

// 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>
2
1

I have used WordPress' custom POST handler functionality before to solve a problem similar to the issue that you are having. Here is documentation from the codex on this: https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)

Basically, you would use an action to handle the post request:

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' );

And modify your form a but so that WordPress knows to use your function to handle the request:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.