How to: Redirect After Login To the Previous Page In WordPress

  • How Tos

Do you want to redirect users back to the page they were viewing after logging in? Sometimes, it is required to redirect users back to the page they were viewing before logging in. Depending on the user’s role, WordPress would either take them to the dashboard or their profile section in the WordPress admin area.

In this guide, we’ll show you how to redirect users after a successful login in WordPress easily. 

Also read: How to Customize WordPress Dashboard Easily

Method 1 – The Manual Way

If you are using the wp_login_form function in your theme to display the login form, then you don’t have to do anything. This function will automatically redirect the user to the previous page. But, if you want to redirect the user to a specific page, you have to use the redirect option for wp_login_form. For example,

if ( ! is_user_logged_in() ) {
	wp_login_form( array( 'redirect' => home_url( 'my-account' ) ) );
}

Suppose you are using the wp_login_url function to display a link or button to the user and redirect the user to the default WordPress login page. Then you can use the below code to redirect the user to the previous page.

<?php
	global $wp;
	$current_url = home_url( add_query_arg( array(), $wp->request ) );
?>
<a href="<?php echo esc_url( wp_login_url( $current_url ) ); ?>">Login</a>

If you want to display the login/logout link in the navigation menu, you may use the code snippet below. Please change the value of theme_location depending on your theme’s navigation. It may generally be primary, main, or similar. It’s strongly recommended to create a child theme for adding the code snippets. You may also use any code snippets management plugin for the same.

function wpcontentco_login_menu_link( $items, $args ) {
	if ( $args->theme_location === 'primary' ) {
		global $wp;
		$current_url = home_url( add_query_arg( array(), $wp->request ) );
		if ( is_user_logged_in() ) {
			// Logout.
			$items .= sprintf( '<li class="menu-item"><a href="%s">Logout</a></li>', esc_url( wp_logout_url( $current_url ) ) );
		} else {
			// Login.
			$items .= sprintf( '<li class="menu-item"><a href="%s">Login</a></li>', esc_url( wp_login_url( $current_url ) ) );
		}
	}
	return $items;
}
add_filter( 'wp_nav_menu_items', 'wpcontentco_login_menu_link', 10, 2 );

Also read: How to Easily Add Custom Code to Your WordPress Websites

Method 2 – By Using a Plugin

LoginWP  (formerly Peter’s Login Redirect) helps you set a redirect URL for post-registration. First download and activate the plugin from the Repository.

 Look for the new LoginWP menu item in your sidebar just below Settings. Navigate to LoginWP > Redirections.

Redirect All Users

To redirect all users to the previous page they were on, use the ‘All Other Users’ section. Set login and logout URLs to {{previous_page}} and save the changes.

Redirect Specific Users

If you want to only redirect specific users back to the previous page they were on, go to Redirection Rules>Add New.

Then choose Rule Condition (Username or User Role or User Capability) and set login and logout URLs to {{previous_page}}. Save the changes.

Why Redirect After Login in WordPress?

Usually, people don’t pay much attention to the smallest things. After login, redirection to the previous page is a very small thing but it’s very effective for a great user experience. A lot of website owners run multi-user WordPress websites. For example, a multi-author blog, a membership community, or an online store.

All these websites require users to login to perform certain actions and access their account details.

Some sites might have very tricky navigation and hard-to-find pages. So when a user comes to your site and tries to access any pages they might not be able to access those pages due to login restrictions. So they will log in to the site and try to find the same page which gives a wrong impression of your site. Instead, we can add simple code that will automatically capture the page before login and will automatically redirect to the page they were viewing before logging in.

Many WordPress membership plugins and eCommerce software automatically handle redirects by showing users custom login pages and redirecting them to a custom account management page. However, some websites may not be using such a plugin. In that case, users will be redirected to the default WordPress admin area. This method doesn’t provide users with complete guidance on what to do next. Though bloggers and writers may find this method easy, it’s not a good user experience for your users and customers.

We hope this article helped you learn how to easily redirect users after successful login in WordPress. You may also want to see our complete WordPress security guide to keep your user data safe and secure. If you liked this article, please subscribe to our Newsletter for more articles.

The WP Week Newsletter

A weekly newsletter covering updates from the WordPress ecosystem that are relevant and helpful for WordPress agencies, developers, and enthusiasts

2 Comments

  1. Thanks very much for this. I tried tons of different code blocks before this; none worked for me. Using the approach in your second snippet, I was able to modify my homemade login link/widget to redirect to the page it appears on.

Leave your comment

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