0

I have written a custom registration form in a Wordpress Page which collects form details and then attempts to submit to the user table in the database.

When I submit, the page (non-intentionally) redirects to the home page and doesn't submit a user.

Can you see what's wrong with my code? I'm using the "Insert PHP" plugin if you are wondering about the "[insert_php]" tags. Also, what's the easiest way to debug forms as you develop?:

[insert_php]

 // Initialise variables
$name = "";
$password = "";
$email = "";
$phone = "";
$how = "";
$type = "";

// Sanitise fields
if($_SERVER["REQUEST_METHOD"] == "POST")
{
    $name = test_input($_POST["inputUsername"]);
    $password = test_input($_POST["inputPassword"]);
    $email = test_input($_POST["inputEmail"]);
    $phone = test_input($_POST["inputPhone"]);
    $how = test_input($_POST["inputHow"]);
    $type = test_input($_POST["inputType"]);
}

// Validate fields
registration_validation(
    $name,
    $password,
    $email,
    $phone,
    $how,
    $type
);

// Attempt to submit to database.
complete_registration(
    $name,
    $password,
    $email,
    $phone,
    $how,
    $type
);

// Strip any special characters out of the input.
function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

// Do specific checks e.g. length of username etc.
function registration_validation($name, $password, $email, $phone, $how, $type) 
{
    global $reg_errors;
    $reg_errors = new WP_Error;

    if (empty($name) || empty($password) || empty($email) || empty($phone) || empty($how) || empty($type))
    { 
         $reg_errors->add('field', 'Required form field is missing');
    }

    if (is_wp_error($reg_errors))
    {
        foreach ($reg_errors->get_error_messages() as $error)
        {
            echo '<div>';
            echo '<strong>ERROR</strong>:';
            echo $error . '<br/>';
            echo '</div>';
        }
    }
}

// Submit to the database.
function complete_registration($name, $password, $email, $phone, $how, $type) 
{
    global $reg_errors;
    if ( 1 > count( $reg_errors->get_error_messages() ) ) 
    {
        $userdata = array(
            'user_login'    =>   $name,
            'user_email'    =>   $email,
            'user_pass'     =>   $password,
            'user_phone'    =>   $phone,
            'user_type'     =>   $type,
            'user_how'      =>   $how
        );
        $user = wp_insert_user( $userdata );
        echo 'Registration complete. Goto <a href="' . get_site_url() . '/wp-login.php">login page</a>.';   
    }
}
[/insert_php]

<form method="post" action="[insert_php] echo htmlspecialchars($_SERVER["PHP_SELF"]);[/insert_php]">
    <div class="form-group">
      <label for="inputUsername">Username</label>
      <input type="text" class="form-control" id="inputUsername">
    </div>
    <div class="form-group">
        <label for="inputPassword">Password</label>
        <input type="password" class="form-control" id="inputPassword">
    </div>
    <div class="form-group">
      <label for="inputEmail">Email</label>
      <input type="email" class="form-control" id="inputEmail" aria-describedby="emailHelp" placeholder="name@example.com">
    </div>
    <div class="form-group">
        <label for="inputPhone">Contact Phone Number</label>
        <input type="password" class="form-control" id="inputPhone">
    </div>
    <div class="form-group">
        <label for="how">How did you hear about us?</label>
        <select class="form-control" id="how">
            <option>Word of mouth</option>
            <option>Local paper</option>
            <option>Search engine</option>
            <option>Other</option>
        </select>
    </div>
    <div class="form-group">
        <label for="signupType">I am a:</label>
        <select class="form-control" id="signupType">
            <option>Parent / Guardian</option>
            <option>Student</option>
        </select>
    </div>
    <button type="submit" value="Submit" class="btn btn-primary">Submit</button>
  </form>
1

$_SERVER["PHP_SELF"] will NOT get you the current URL in WordPress, but will return /index.php (as that's the PHP script that is being executed). WordPress then doesn't know what to do with the data that has been submitted and decides to redirect to the home page.

If you want to submit to the current page, you can just leave the action attribute empty, e.g.

<form action="" method="POST">

What you are doing will work, but it's not really the WordPress way of doing things, you might want to look into developing a plugin and using AJAX (you don't have to have a plugin for that, you can simply define actions in your theme's functions.php).

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.