WooCommerce: Login Redirect to Previous URL @ My Account
We’ve already seen how to set a custom My Account login redirect URL by user role – but today we want to cover another case scenario: redirecting users to the previous URL (referrer) after logging in from the My Account page.
Actually, WooCommerce already prints a hidden input field (“_wp_http_referer“) in the login form thanks to wp_nonce_field(), but for some reason this is not enough to allow the actual redirect.
Thankfully, the WooCommerce process_login() function provides another workaround: if $_POST[‘redirect’] is set, the form will consider redirecting to this URL instead of the default My Account Dashboard! This means we can simply add a new hidden field to the login form with that exact name (“redirect”), so that we can make that function trigger the redirect we want.
Easier coded than said, so let’s see how it’s done. Enjoy!
With the snippet you find below I’ve added a hidden input field to the WooCommerce My Account login form with name = “redirect”. The input value is populated with the previously visited URL and this will be the URL the login form will redirect to upon successful login.
PHP Snippet: Redirect Users to Previous (“Referrer”) URL
You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"
Does this snippet (still) work?
Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.
If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)
Need Help with WooCommerce?
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!
Rodolfo Melogli
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.
15 thoughts on “WooCommerce: Login Redirect to Previous URL @ My Account”
Stephen
Hi Rodolfo, it works perfectly for me, thanks!
I’ve realised one thing though. If someone does a password reset, they are then presented with login page so they login with new password and get taken back to password reset page as obviously that was the previous page.
I was thinking maybe a workaround could be a snippet so that when they reset password they then get taken to homepage instead of login. Then from home they can go to login with their newly set password. That way the login redirect will then take them back to home. Is it possible?
I tried using this snippet and it works when I use the Elementor login form, however, when I use the basic WordPress login form (or maybe it’s the theme, it’s a popup), it doesn’t seem to redirect.
It seems like the popup login is linking to the my-account page right away. So maybe it’s overriding that. Looking forward to hearing from you and seeing if there is a way around it. As I agree that having a user go to page, login and then have to find that page again – that’s annoying and your solution is pretty awesome.
How can we do the same for customers that register for the first time? It would need to work for both new customers creating an account and returning customers logging in.
Actually, based on Rodolfo’s visual hook guide for my-account page (I LOVE these visual hook guides!) https://www.businessbloomer.com/woocommerce-visual-hook-guide-account-pages/ I could figure out the snippet that makes it work to do the same redirect when creating a new account. 🙂
Probably someone can combine the two functions into one but for now I use two like this:
//For login button:
add_action( 'woocommerce_login_form_end', 'bbloomer_actual_referrer' );
function bbloomer_actual_referrer() {
if ( ! wc_get_raw_referer() ) return;
if ( is_checkout() ) return;
echo '';
}
//For create account button:
add_action( 'woocommerce_register_form_end', 'bbloomer2_actual_referrer' );
function bbloomer2_actual_referrer() {
if ( ! wc_get_raw_referer() ) return;
if ( is_checkout() ) return;
echo '';
}
Questions? Feedback? Support? Leave your Comment Now! _____
If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)
With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.
Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.
Hi Rodolfo, it works perfectly for me, thanks!
I’ve realised one thing though. If someone does a password reset, they are then presented with login page so they login with new password and get taken back to password reset page as obviously that was the previous page.
I was thinking maybe a workaround could be a snippet so that when they reset password they then get taken to homepage instead of login. Then from home they can go to login with their newly set password. That way the login redirect will then take them back to home. Is it possible?
Thanks for your site, it’s really great!
Good point. You could maybe check that wc_get_raw_referer() is not the Password Reset URL maybe?
It works perfectly in PC mode, it doesn’t work for me on mobile devices.
All the best
This should not happen – try clear your mobile browser cache/cookies
I tried using this snippet and it works when I use the Elementor login form, however, when I use the basic WordPress login form (or maybe it’s the theme, it’s a popup), it doesn’t seem to redirect.
It seems like the popup login is linking to the my-account page right away. So maybe it’s overriding that. Looking forward to hearing from you and seeing if there is a way around it. As I agree that having a user go to page, login and then have to find that page again – that’s annoying and your solution is pretty awesome.
Hi Vitaliy, and what about the WooCommerce login form?
Hi Rodolfo,
Would this also work for users who are registering?
Thanks in advance
I believe not
How can we do the same for customers that register for the first time? It would need to work for both new customers creating an account and returning customers logging in.
Thanks!
Actually, based on Rodolfo’s visual hook guide for my-account page (I LOVE these visual hook guides!) https://www.businessbloomer.com/woocommerce-visual-hook-guide-account-pages/ I could figure out the snippet that makes it work to do the same redirect when creating a new account. 🙂
Probably someone can combine the two functions into one but for now I use two like this:
Top!
Great! Just to add, you can combine for login and reg by attaching the same function to both login or reg form hooks…
Cool thank you
There is also login form in the checkout page.
Thanks!