Wordpress two step registration form Wordpress two step registration form wordpress wordpress

Wordpress two step registration form


STEP 1

First you should create two separate templates ( one for each step ).In first template you should create a form that will send user email to the second page. The link should have GET attributes so you can get his email and First name. Here is an example ( note that it can be improved ):

<?php /*** Template Name: Step 1*/get_header();if ( !empty( $_POST['firstname'] ) && !empty( $_POST['email'] ) ) {    $link = 'http://my-site/step-2';    $link = add_query_arg(        array(            'firstname' => $_POST['firstname'],            'email'     => $_POST['email'],        ),        $link    );    $subject = 'New user registration';    $message = 'Please click on the following link to complete your registration: ' . $link;    $headers = array('Content-Type: text/html; charset=UTF-8');    $result = wp_mail( $_POST['email'], $subject, $message, $headers );    if ( $result ) {        $message = 'Please check your email address to complete the registration';    } else {        $message = 'Something went wrong. Please contact the administrator';    }    echo $message;} else {?>    <form method="POST" action="">        <input type="text" name="firstname" placeholder="First Name">        <input type="email" name="email" placeholder="Email address">        <input type="submit" value="Submit">    </form><?php}get_footer();

We create a simple check if the form is submitted and all the fields are filled.If so we can send an email to step 2.


STEP 2

We will create a separate template where we will fill the data from first one using $_GET, and we will add two new fields ( username and password ) that will be empty.

<?php /*** Template Name: Step 2*/get_header();if ( !empty( $_POST['firstname'] ) && !empty( $_POST['email'] ) && !empty( $_POST['password'] ) ) {    $user_id = username_exists( $_POST['username'] );    if ( !$user_id and email_exists($_POST['email']) == false ) {        $user_id = wp_create_user( $_POST['username'], $_POST['password'], $_POST['email'] );        if ( $user_id ) {            update_user_meta($user_id, 'first_name', $_POST['firstname']);            $message = 'User has been created';        }    } else {        $message = 'User already exists!';    }    echo $message;} else {?>    <form method="POST" action="">        <input type="text" name="firstname" value="<?php echo ( !empty( $_GET['firstname'] ) ) ? $_GET['firstname'] : '' ; ?>" placeholder="First Name">        <input type="email" name="email" value="<?php echo ( !empty( $_GET['email'] ) ) ? $_GET['email'] : '' ; ?>" placeholder="Email Address">        <input type="text" name="username" placeholder="Username">        <input type="password" name="password" placeholder="Password">        <input type="submit" value="Submit">    </form><?php}get_footer();

If the second form is submitted and everything is ok we can create the user.Once created we can update its first name.

You can make unlimited modifications to my code, but this is the base. For example, we can make the fields required, we can check the password strength, the name length etc.


Here's a short brief of the steps you can take.

Make sure the user is unique and store the credentials for the confirmation.

if(!email_exists( $email )){    /*       Store credentials in a custom table        with a unique identifier,        hashed password and email.        Email user with the confirmation link ex.       site.com/confirmation/<unique-identifier>    */}

In the confirmation page create the user simply by:

// Confirm <unique-identifier>// Create user$user_id = wp_create_user( $email, $password, $email );// Set user role$user = new WP_User( $user_id );$user->set_role( 'contributor' ); // or a custom role


I think for now you can try this Contact form 7 Multi-Step Form plugin down here :

Contact Form 7 Multi-Step Forms

Try demo here

Later you can get to know how to develop such registration form by your talent