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!
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.
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!
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”
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?
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.
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:
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.