How To Auto-Login & Redirect WordPress Users After Registration?

How To Auto-Login & Redirect WordPress Users After Registration?

November 4, 2024

Creating a smooth registration process for your WordPress site can significantly improve user experience. In this short article you will learn how to automatically log in your new WordPress users and redirect them to any page you choose.

Have you ever signed up for a website and experienced that frustrating moment when you have to log in right after registering? It is like being invited into someone’s house, only to find yourself standing at another locked door.

WordPress Sign-Up Form

How Do Users Sign Up for WordPress Website?

When someone registers on your WordPress site, they typically go through a very standardized process.

  1. Fill Out Registration Form: First, they need to fill out the registration form.
  2. Email Verification: Then, they need to check their email for a verification link.
  3. Manual Login: After clicking that link, they still need to log in manually.

This multi-step process, while secure, can get boring and annoying. It is no surprise that some users abandon your website before even getting started.

If you do not want to install a new plugin for something as simple as this, you can use a simple code snippet.

ℹ️
You can add the snippet by editing your theme’s functions.php file, creating a child theme, or installing a plugin like Code Snippets.

Solution

The snippet below will automatically handle everything from the moment a user registers. No more manual logins, no more confusion about where to go next.

Instead, your users will go through a simple registration process that ends on page you specify - a welcome guide, a dashboard, or any other one.

Auto-Login & Redirect After Registration

Let’s break down the code into two main parts. It uses WordPress’s built-in authentication mechanisms to ensure security compliance.

  1. Automatic Login: After a user registers, the code will automatically log them in by verifying their credentials and setting the necessary cookies.
  2. Redirection to a Specified Page: Once logged in, the code redirects users to a page of your choice.

The code is triggered by the user_register action, which is called whenever a new user registers on your WordPress site.

The $redirect_url variable specifies where to redirect the user. This can be any page you choose. By using get_permalink() you can redirect users to the page with ID 12, or you can replace this variable with a URL string.

Below is the full code snippet, which you can add to your child theme file or through a code snippet plugin.

function bis_autologin_user_registration( $user_id ) {
	// Make sure we have a valid user
	if ( ! $user_id || ! is_numeric( $user_id ) ) {
		return;
	}

	// Get user information
	$user = get_user_by( 'id', $user_id );
	if ( ! $user ) {
		return;
	}

	// Log them in automatically
	wp_set_current_user( $user_id );
	wp_set_auth_cookie( $user_id, true );
}
add_action( 'user_register', 'bis_autologin_user_registration', 10, 1 );

function bis_redirect_after_signup( $redirect_url, $user_id ) {
	// Set up where you want to send the users after the registration process is completed
	// Use the ID of the page, e.g. 12 or replace $redirect_url with URL

	$redirect_url = get_permalink( 12 );
	// $redirect_url = 'https://example.com/getting-started/';

	return $redirect_url;
}
add_filter( 'registration_redirect', 'bis_redirect_after_signup', 10, 2 );