Reserve Your Free Seat for Our Next WooCommerce Class! Search
Business Bloomer
  • Join
  • WooCommerce Plugins
  • WooCommerce Tips
  • Log In
  • 0
  • Business Bloomer Club
  • WooCommerce Plugins
  • WooCommerce Tips
  • Log In
  • Search
  • Contact
  • Cart
WooCommerce Code Snippets Customer Login My Account Redirect

WooCommerce: Login Redirect to Previous URL @ My Account

Last Revised: Jul 2024

STAY UPDATED

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

/**
 * @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' ) ) . '" />';
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by…
  • WooCommerce: Separate Login, Registration, My Account Pages
    There are times when you need to send logged out customers to a Login page and unregistered customers to a standalone Register page. As you…
  • WooCommerce: Add New Tab @ My Account Page
    One of the features of Business Bloomer Club is the provision of Premium WooCommerce Q&A Support to supporters who enroll. So, how to add an…
  • WooCommerce: How To Make A Website GDPR Compliant? (12 Steps)
    Ok, we all know that the EU General Data Protection Regulation (GDPR) will come into force on the 25th May 2018. So the main question…
  • WooCommerce Visual Hook Guide: My Account Pages
    Hey WooCustomizers, the Visual Hook Guide is back 🙂 In this episode, I’ve created a visual HTML hook guide for the WooCommerce Account Pages (there…

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. Follow @rmelogli

19 thoughts on “WooCommerce: Login Redirect to Previous URL @ My Account”

  1. Stephen
    January 30, 2023

    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!

    Reply
    1. Rodolfo Melogli
      January 31, 2023

      Good point. You could maybe check that wc_get_raw_referer() is not the Password Reset URL maybe?

      Reply
      1. tomas.waterluxe
        July 16, 2024

        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?

        Reply
        1. Rodolfo Melogli
          July 23, 2024

          Very good point. Can you try the new version please?

          Reply
          1. Tomás Moya
            September 26, 2024

            Perfecto Rodolfo, ahora síiii

            Grande!! 🙂

            Reply
            1. Rodolfo Melogli
              October 11, 2024

              Awesome!

              Reply
  2. Tomás
    January 10, 2023

    It works perfectly in PC mode, it doesn’t work for me on mobile devices.

    All the best

    Reply
    1. Rodolfo Melogli
      January 13, 2023

      This should not happen – try clear your mobile browser cache/cookies

      Reply
  3. Vitaliy Lyubezhanin
    September 15, 2022

    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.

    Reply
    1. Rodolfo Melogli
      September 27, 2022

      Hi Vitaliy, and what about the WooCommerce login form?

      Reply
  4. paul triggs
    May 24, 2021

    Hi Rodolfo,

    Would this also work for users who are registering?

    Thanks in advance

    Reply
    1. Rodolfo Melogli
      June 3, 2021

      I believe not

      Reply
      1. Judith Kocken
        October 10, 2021

        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!

        Reply
        1. Judith Kocken
          October 10, 2021

          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 '';
          }
          
          Reply
          1. Rodolfo Melogli
            October 12, 2021

            Top!

            Reply
            1. Simon
              January 18, 2023

              Great! Just to add, you can combine for login and reg by attaching the same function to both login or reg form hooks…

              function bbloomer_actual_referrer() {
                 if ( ! wc_get_raw_referer() ) return;
                 echo '';
              }
              
              add_action( 'woocommerce_login_form_end', 'bbloomer_actual_referrer' );
              add_action( 'woocommerce_register_form_end', 'bbloomer_actual_referrer' );
              
              Reply
              1. Rodolfo Melogli
                January 31, 2023

                Cool thank you

                Reply
  5. Elchanan Levavi
    May 12, 2021

    There is also login form in the checkout page.

    function bbloomer_actual_referrer() {
       if ( ! wc_get_raw_referer() ) return;
       if ( is_checkout() ) return;
       echo '<input type="hidden" name="redirect" value="' . wp_validate_redirect( wc_get_raw_referer(), wc_get_page_permalink( 'myaccount' ) ) . '" />';
    }
    Reply
    1. Rodolfo Melogli
      June 2, 2021

      Thanks!

      Reply
Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

Cancel reply

Your email address will not be published. Required fields are marked *


Search WooCommerce Tips

Popular Searches: Visual Hook Guides - Checkout Page - Cart Page - Single Product Page - Add to Cart - Emails - Shipping - Prices - Hosting

Recent Articles

  • WooCommerce: Save Order Currency Exchange Rate
  • WooCommerce: Get Orders Containing a Specific Product
  • WooCommerce: Sort Order Items by Name, SKU, Total, Quantity
  • WooCommerce: Add Buy Now Button @ Single Product Page
  • WooCommerce: Automatically Log Out Customers After Checkout

Latest Comments

  1. Marco on WooCommerce: Remove “Payments” From WordPress Sidebar Admin Menu
  2. Rodolfo Melogli on WooCommerce: Automatically Register Guest Checkouts
  3. Rodolfo Melogli on Moving the WooCommerce Coupon Field to a Custom Location on the Checkout Page

Find Out More

  • Become a WooCommerce Expert
  • Business Bloomer Club
  • WooCommerce Blog
  • WooCommerce Weekly
  • Contact

Contact Info

Ciao! I'm Rodolfo Melogli, an Italian Civil Engineer who has turned into an international WooCommerce expert. You can contact me here:

Twitter: @rmelogli

Get in touch: Contact Page

Business Bloomer © 2011-2025 - VAT IT02722220817 - Terms of Use - Privacy Policy

Cart reminder?

x