User registration problem in WordPress

I am trying to make a user registration form in my site’s side bar. Users are able to register but instead of getting a success message, WordPress usually redirect them to 404 page.

Below is my form:

<form id="joinmaillist"   method='post'>
    <table>
        <tr>
            <td>Name</td>
            <td><input type="text" placeholder="Your name here.." name="name" title="Please enter your name" required/></td>
        </tr>
        <tr>
            <td>Email</td>
            <td><input type="email" name="email" placeholder="Your email here.." required/></td>
        </tr>
        <tr>
            <td></td>
            <td style="text-align:right"><input class="submitmail" type="submit" value="Join!" name="joinm"/></td>
        </tr>
    </table>
</form>

and below is my PHP code:

<?php
if (isset($_POST['joinm'])){
    $user_name=$_POST['name'];
    $email =$_POST['email'];


$user_id=  username_exists($user_name);

    if (!$user_id){
        $random_pass = wp_generate_password(12, false);
        $user_id=wp_create_user($user_name, $password, $email);
        header('Location:' . $_SERVER['REQUEST_URI']);
    }else{

    }
}

1 Answer
1

Look at the code in wp-login.php (line 481 and following). There you can see how registration works.

The names of your form – email, name – are probably overwritten by WordPress. Always use prefixed names in forms to avoid collisions, eg.: ma_email and ma_name.

Prepare incoming data. Do not just write anything someone sends you into your data base. It may be dangerous.

Oh, and welcome to WordPress Stack Exchange! 🙂

Leave a Comment