Creating wordpress user registration form

You are sending the data from the form directly to TestForm.php file, which is a PHP script outside of WordPress logic. It is and independent script. You could set the form’s action attribute to a empty string, this way the form data is sent to same page that contains the form, which is part of WordPress:

<div id="container">
    <form method="post" name="myForm">
        User <input type="text"  name="uname" />
        Email  <input id="email" type="text" name="uemail" />
        Password  <input type="password"  name="upass" />
        <input type="submit" value="Submit" />
    </form>
</div>

In this case, the form data is sent to a WordPress page and the add_action('init','create_account'); is triggered if you add it to functions.php file, for example like this:

add_action('init','create_account');
function create_account(){
    //You may need some data validation here
    $user = ( isset($_POST['uname']) ? $_POST['uname'] : '' );
    $pass = ( isset($_POST['upass']) ? $_POST['upass'] : '' );
    $email = ( isset($_POST['uemail']) ? $_POST['uemail'] : '' );

    if ( !username_exists( $user )  && !email_exists( $email ) ) {
       $user_id = wp_create_user( $user, $pass, $email );
       if( !is_wp_error($user_id) ) {
           //user has been created
           $user = new WP_User( $user_id );
           $user->set_role( 'contributor' );
           //Redirect
           wp_redirect( 'URL_where_you_want_redirect' );
           exit;
       } else {
           //$user_id is a WP_Error object. Manage the error
       }
    }

}

P.D.: I suggest you to use a modern HTML5 form markup and input types

Leave a Comment