Home > Blockchain >  Wordpress Custom Registration $_POST not working
Wordpress Custom Registration $_POST not working

Time:11-01

So, I'm doing custom registration in custom theme in Wordpress, I've done logging in with the same method, and it works just fine, but for some reason registering doesn't work, because when the request is submitted on /registration/ to /registration/ the $_POST is not working.

My code

<?php
/* 
Template Name: Registration
*/
?>
<?php
global $wpdb, $user_ID;  
if ($_POST) {
    // GETTING VALUES

    // VALIDATION

    // Creating user if no errors
}

get_header();   
  
?>
 <form class="form" method="POST" action="<?php echo $_SERVER['REQUEST_URI']" />
     // FORM STUFF
 </form>
?>

For the sake of minimal example I removed code inside of $_POST condition, because it newer gets executed. The POST request gets sent to the correct URl, but returns 404. Sending GET request to the same URL results in displaying of this page, as it should. Any ideas ?

CodePudding user response:

It is because the truth value of $_POST itself is false even when it has values inside it. For instance if you will do the following

<?php var_dump( $_POST === true ); ?>

The program will return a false.

You should be testing the following proposition instead:

<?php 
if( isset( $_POST ) ){
  // Your code here if form is submitted.
}
?>

I have also noticed there are some mistakes in the form attributes, It should be

 <form class="form" method="POST" action="<?php echo $_SERVER['REQUEST_URI']; ?>" >
    
 </form>

Instead of

<form class="form" method="POST" action="<?php echo $_SERVER['REQUEST_URI']" />
     // FORM STUFF
 </form>
  • Related