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!
PHP Snippet: Redirect Users to Previous (“Referrer”) URL
/**
* @snippet Redirect to Referrer @ WooCommerce My Account Login
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer, BusinessBloomer.com
* @testedwith WooCommerce 9
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_login_form_end', 'bbloomer_actual_referrer' );
function bbloomer_actual_referrer() {
if ( ! wc_get_raw_referer() ) return;
if ( strpos( wc_get_raw_referer(), "lost-password" ) !== false ) return;
echo '<input type="hidden" name="redirect" value="' . wp_validate_redirect( wc_get_raw_referer(), wc_get_page_permalink( 'myaccount' ) ) . '" />';
}
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?
What Esteban comments seems super important to me. Redirecting the client to the previous URL is great, it’s how it should be.
However, I receive many calls for the reason Esteban mentions when it comes to resetting the password. You should include a filter so that you don’t have that behavior on password reset.
The client enters the password twice, clicks the save button and returns to the password reset page.
Any solution?
Very good point. Can you try the new version please?
Perfecto Rodolfo, ahora sÃiii
Grande!! 🙂
Awesome!
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!