How to Redirect Users After Login and Logout in WordPress

In this tutorial, you will learn how to effectively redirect users after they log in and log out of your WordPress website. Redirecting users not only improves user experience but also plays a crucial role in guiding visitors to specific pages, thus enhancing engagement with your content. This guide provides step-by-step instructions for implementing user redirection without relying on plugins. Let’s dive into the details and explore the process of redirecting users after login and logout on your WordPress site.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Redirect Users After Login

Start by accessing the functions.php file of your WordPress child theme. This file acts as the control center for customizing your theme’s features. You can make direct edits using the Theme Editor in your WordPress dashboard, or opt for more advanced editing using an external code editor alongside FTP (File Transfer Protocol). Insert the provided code snippet at the end of the file to incorporate the desired functionality.

// Redirect user after login using wp_login action
function custom_login_redirect( $redirect_to, $request, $user ) {
	// Is there a user to check?
	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
		// Check for admins
		if ( in_array( 'administrator', $user->roles ) ) {
			// Redirect admins to the dashboard
			return home_url( '/dashboard' );
		} else {
			// Redirect other users to the home page
			return home_url();
		}
	} else {
		return $redirect_to;
	}
}
add_action( 'wp_login', 'custom_login_redirect', 10, 3 );
Code language: PHP (php)

Explanation:
The custom_login_redirect function is hooked to the wp_login action and checks the user’s role. If the user is an administrator, they are redirected to the /dashboard page. For other users, they are redirected to the home page. The wp_login action hook triggers after a user successfully logs in.

  • wp_login action: Triggered when a user logs in.
  • $redirect_to: The default redirect destination after login.
  • $request: The URL the user is coming from.
  • $user: User object containing user information.

Redirect Users After Logout

Open your child-theme’s functions.php file again and add the following code snippet at the end of the file:

// Redirect user after logout using wp_logout action
function custom_logout_redirect() {
	// Redirect all users to the home page after logout
	wp_redirect( home_url() );
	exit();
}
add_action( 'wp_logout', 'custom_logout_redirect' );
Code language: PHP (php)

Explanation:

The custom_logout_redirect function is hooked to the wp_logout action and redirects all users to the home page after logout.

  • wp_logout action: Triggered when a user logs out.
  • wp_redirect(): Redirects the user to a specified URL.
  • exit(): Terminates the script immediately after redirection.

Save the functions.php File

After adding the provided code snippets to your theme’s functions.php file, don’t forget to save the file to apply the changes. Now, test the redirections by logging in and out of your WordPress website to see if they work as intended. This simple step ensures that your login and logout redirections are active and functional on your site.

That’s it! You’ve successfully learned how to redirect users after login and logout on your WordPress website. By implementing these redirections, you can create a seamless user experience, ensuring that visitors are directed to specific pages based on their actions, ultimately enhancing user engagement.

Leave your feedback and help us improve ๐Ÿถ

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee โ˜•๏ธ or donating via PayPal ๐Ÿ’ฐ.

More free knowledge, because why not?

Your thoughts matter, leave a reply ๐Ÿ’ฌ

Your email address will not be published. Required fields are marked *