How to Redirect Users to the Previous Page Upon Login in WordPress
In WordPress, it is common to redirect users to specific pages after they log in, such as the dashboard, a custom page, or the homepage. However, sometimes you may want to redirect users to the page they were previously on, so they can continue where they left off. This article will guide you through the process of redirecting users to the previous page upon login in WordPress without using a plugin.
Why should Redirect users to the previous page upon login?
Redirecting users to the previous page upon login in WordPress can offer several benefits and enhance the user experience on your website.
Improved User Experience
One of the main advantages of redirecting users to the previous page upon login is that it improves the user experience on your website. If a user was in the middle of reading an article or making a purchase when they were prompted to log in, redirecting them back to the page they were on will allow them to continue where they left off without any interruption. This can help reduce frustration and keep users engaged on your site.
Enhanced Navigation
Redirecting users to the previous page upon login can also enhance the navigation on your website. By allowing users to continue where they left off, they won’t need to start from the beginning of a page or try to find their way back to where they were before they logged in. This can help improve the overall user experience and make it easier for users to navigate your website.
Increased Conversions
Redirecting users to the previous page upon login can also increase conversions on your website. For example, if a user was in the process of making a purchase when they were prompted to log in, redirecting them back to the checkout page will allow them to complete the purchase without any interruptions. This can help improve the conversion rate on your website and increase sales.
Steps to Redirect Users to the previous page upon login.
Please note that do the customization in your child theme. If you haven’t yet created child theme then read this article to create one. If you do not want to edit functions.php file then install a plugin Code Snippets by Code Snippets Pro, and add the code by using this plugin.
Step 1: Access functions.php file.
Go to Appearance > Theme File Editor > Theme Functions (functions.php). Below is the image for your reference.
Step 2: Add below code.
Copy below code and paste at the end of functions.php file.
// Redirect users to previous page upon login
if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) {
add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
wp_safe_redirect($location);
exit();
}
}
Finally, test the redirection by logging in and out of your WordPress site. You should be redirected to the page you were previously on every time you log in.
Conclusion
By following these steps, you can easily redirect users to the previous page upon login in WordPress without using a plugin. This method uses WordPress’ built-in functions and hooks, and it provides a simple and reliable way to redirect users to the page they were previously on. This can help improve the user experience on your WordPress site and allow users to continue where they left off.
More Articles:
Encode Email Address to prevent Spam in WordPress.
Allow SVG File Upload in WordPress.
Create Recent Posts Custom Dashboard Widget.