How to: Redirect To Previous Page In WordPress After Login

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 easily redirect users after a successful login in WordPress. 

Why Redirect Users 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.

There are many WordPress membership plugins and eCommerce software that 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. In order to avoid that, let’s take a look at how to easily redirect users after successful login in WordPress. 

Steps For Login Redirect To The Previous Page

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, then you have to use 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.

	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>

By Using a Plugin

Ultimate Member is a WordPress profile and membership plugin which has been active on more than two lakh sites. It’s a free plugin but it’s possible to extend the features by using (premium) extensions. You can specify the redirection URL after registration or login under Ultimate Member > User Roles.

You can choose one from the following options for Action to be taken after login:

  • Redirect to profile
  • Redirect to URL
  • Refresh active page
  • Redirect to WordPress Admin

Among them, if you choose the Refresh active page option, users will be redirected to the Account page instead of the previous page (or referring page) after login.

If you want to redirect users to the previous page (or current active page before login attempt), add the following code to your theme’s function file (i.e. functions.php). Of course, it’s strongly recommended to create a child theme. Also change the value of theme_location depending on your theme’s navigation. It may be generally ‘primary’, ‘main’ or similar.

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, then please subscribe to our Newsletter for more articles.

Leave your comment

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

⚡️ Weekly WordPress Updates!

News, deals and exclusive updates, delivered to your inbox every Tuesday. Subscribe only if you love WordPress. We hate spam too.

Holler Box