Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress two step registration form

I'm trying to achieve the following and I don't know from where to start.

I'm trying to create a sign up/registration form with two steps.

First step: is to have 2 input fields(Name, Email) and when the user submits, an Email is sent with a link to step two.

Step two: a user enters the link sent to his email, enters a page with a second form. that has a value of the email and name he used, + 2 other fields( Username, Password) in which he will have access to certain pages with.

I could not find where to start and no plugins meet the following.

Regards,

Mostafa.

like image 401
Mostafa Mohsen Avatar asked Nov 20 '16 18:11

Mostafa Mohsen


2 Answers

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.

like image 82
Stanimir Stoyanov Avatar answered Oct 19 '22 22:10

Stanimir Stoyanov


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
like image 31
Stubbies Avatar answered Oct 19 '22 23:10

Stubbies