Home » After Login Redirect To Previous Page In WordPress

After Login Redirect To Previous Page In WordPress

Last updated on July 1, 2020 by

Sometimes, It is required to redirect users back to the page they were viewing before logging in. There could be a plugin for this but we are not suggesting to use a plugin for small things. So that we have created a code snippet that will help you after login redirect to previous page in WordPress. Let’s just dive into it.

Table of Contents
1. An Ideal Place to Add Code in WordPress
2. Why We Should Do Login Redirect To Previous Page?
3. Two Steps For Login Redirect To The Previous Page
3.1. Capture Last Page URL in WordPress
3.2. After Login Redirect User To The Last Page

An Ideal Place to Add Code in WordPress

Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.

If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.

Please also note that we have tested all codes in the GeneratePress child theme & Storefront child theme.

Why We Should Do Login Redirect To Previous Page?

Normally, people are not drawing enough attentions to the smallest things. After login redirection to the previous page is very small thing but it’s very effective for the great user experience.

Some sites might have very tricky navigation and hard to find pages so when a user comes to the site and try to access any pages but they can’t access 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 of, we can add simple code that will automatically capture the page before login and it will automatically redirect to the page they were viewing before logging in.

Two Steps For Login Redirect To The Previous Page

We can achieve this via simple 2 steps:

  • First, we need to capture the page they were viewing before logging in.
  • Second is to redirect user to the last or previous page.

Let’s just do it.

01 Capture Last Page URL in WordPress

In the following code, we have used the wp action hook. Which is one effective place to perform any high-level filtering or validation, following queries, but before WordPress does any routing, processing, or handling.

Simply, the following code will set the last visited page URL into the session variable on each page visit.

add_action( 'wp', 'sc_capture_before_login_page_url' );
function sc_capture_before_login_page_url(){
    if( !is_user_logged_in() ):
	$_SESSION['referer_url'] = get_the_permalink();
    endif;
}

02 After Login Redirect User To The Last Page

In the following code, we have used the login_redirect filter hook. As the name suggests, this hooks helpful to change the login redirect URL.

So we will fetch the URL that we captured in the above code snippet and return to the filter. That’s it.

/*@ After login redirection */
if( !function_exists('sc_after_login_redirection') ):
    function sc_after_login_redirection() {

	$redirect_url = home_url('/');
	if ( isset($_SESSION['referer_url']) ):
		$redirect_url = $_SESSION['referer_url'];
		unset( $_SESSION['referer_url'] );
	endif;

	return $redirect_url;
	exit;
   }
   add_filter('login_redirect', 'sc_after_login_redirection');
endif;

Additionally, read our guide on How to Allow Preview of Draft Post Without Login in WordPress

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂. Keep Smiling! Happy Coding!

 
 

Leave a Comment