Simplifying the registration process can significantly improve user experience. Security measures like email verification improve security but can make the process longer, which could discourage some users.
Upon registration, WordPress users often need to log in again, which some of them find inconvenient. In this short article you will learn how to automatically log in your new WordPress users and redirect them to any page you choose.
How Do Users Sign Up for WordPress Website?
When someone registers on your WordPress site, they typically go through a very standardized process.
- Fill Out Registration Form:
First, they need to fill out the registration form. - Email Verification:
Then, they need to check their email for a verification link. - 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.
Solution
The snippet below will automatically handle everything from the moment a user registers. As a result, your users will go through a simple registration process that ends on page you specify - a welcome guide, a dashboard, or any other one.
You can apply the code below without the need for extra plugins, provided you are using a child theme (see "Implementation Options" section for other possibilities). To sum up, by applying the code below you can:
- Eliminate the need to manually log in after the users complete registration.
- Direct users immediately to relevant page (e.g. onboarding content).
- Keep WordPress's built-in security mechanisms.
Auto-Login & Redirect After Registration
The code is simple and can be customized to meet your specific needs. It has two main components, with security handled by WordPress’s built-in authentication system:
- Automatic Login:
After a user registers, the code will automatically log them in by verifying their credentials and setting the necessary cookies. - 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 pm_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', 'pm_autologin_user_registration', 10, 1 );
function pm_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', 'pm_redirect_after_signup', 10, 2 );
Implementation Options
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 implement this code in several ways:
- Child Theme Integration:
Add the code to your child theme's functions.php file. - Custom Plugin:
Create and upload mini-plugin for extra code snippets. - Code Snippets Plugin:
Use a plugin like Code Snippets for easier management
Leave a Reply