How to Automatically Log in Users After Registration

Introduction

Do you have a registration form on your site and want to automatically log in your users after they register? This can save your members a step in the process when they register to your site using the WPForms User Registration addon.

If you automatically approve any user that registers to your site, it can help save time to just log them in after the registration is complete. In this tutorial we’ll walk you through the steps of how to do this by starting with setting up your form all the way to adding the code snippet needed to automatically log them in after registration.

Creating your form

If you need any help creating a registration form, please take a look at this documentation.

Once you’ve created your registration form, there are a few settings that we’ll need to change to set up the automatic log in process for your members.

  1. Go to Settings » User Registration and make sure theĀ Enable user activation is not be checked.
  2. For the best user experience, set the form confirmation to redirect the user to a new page. Redirecting them to a Thank You page, for example, will clearly show they are logged in. To automatically log in users, make sure you've the Enable user activation is not checked

Adding the code snippet

Next, we’ll need to add a small code snippet to your site to complete the automatic login process.

If you need any help adding code snippets to your site, please review this tutorial.

/**
 * Automatically log in user after the registration form is submitted.
 *
 * @link https://wpforms.com/developers/automatically-log-in-users-after-registration/
 *
 */

function wpf_dev_autologin( $user_id, $fields, $form_data, $userdata ) {

    if ( empty( $userdata[ 'user_login' ] ) || empty( $userdata[ 'user_pass' ] ) ) {
        return;
    }

    wp_signon( 
        array(
            'user_login'    => $userdata[ 'user_login' ],
            'user_password' => $userdata[ 'user_pass' ],
            'remember'      => false,
        )
    );
}
add_action( 'wpforms_user_registered', 'wpf_dev_autologin', 10, 4 );

To break down this snippet, once the action to register the user is complete, this snippet will pass along the username and password to the login form which automatically logs in the user thereby eliminating this step.

And that’s it! After completing the registration form, your users will automatically be logged in. Would you like to customize the email that is sent out during the registration process? Take a look at the article How to Change User Email for User Registration Addon.

Action Reference: wpforms_user_registered

FAQ

Q: How can I target specific forms?

A: To target a specific form, simply use this code snippet instead.

/**
 * Automatically log in user after registration form is submitted.
 *
 * @link https://wpforms.com/developers/automatically-log-in-users-after-registration/
 *
 */

function wpf_dev_autologin( $user_id, $fields, $form_data, $userdata ) {

    // Optional, you can limit to specific forms. Below, we restrict output to
    // form ID #1423.
    if ( absint( $form_data['id'] ) !== 1423 ) {
        return;
    }

    if ( empty( $userdata[ 'user_login' ] ) || empty( $userdata[ 'user_pass' ] ) ) {
        return;
    }

    wp_signon( 
        array(
            'user_login'    => $userdata[ 'user_login' ],
            'user_password' => $userdata[ 'user_pass' ],
            'remember'      => false,
        )
    );
}
add_action( 'wpforms_user_registered', 'wpf_dev_autologin', 10, 4 );