Business Bloomer
  • About
  • WooCommerce Blog
  • Online Courses
  • Login
  • 0
  • About
  • WooCommerce Blog
  • Online Courses
  • Login
  • 0

WooCommerce: Login Redirect to Previous URL @ My Account

> Published: Dec 2020 - Revised: Dec 2021
> Blog Category: WooCommerce Tips
> Blog Tags: Customer Login, My Account, Redirect
> Blog Comments: 15 Comments
Tweet

Join 17,000+ WooWeekly subscribers

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        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, BusinessBloomer.com
 * @testedwith    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_login_form_end', 'bbloomer_actual_referrer' );

function bbloomer_actual_referrer() {
	if ( ! wc_get_raw_referer() ) return;
	echo '<input type="hidden" name="redirect" value="' . wp_validate_redirect( wc_get_raw_referer(), wc_get_page_permalink( 'myaccount' ) ) . '" />';
}

Related posts:

  1. WooCommerce: Login Redirect by User Role @ My Account
  2. WooCommerce: Separate Login, Registration, My Account Pages
  3. WooCommerce: Redirect My Account Tab to URL
  4. WooCommerce: Add New Tab @ My Account Page
  5. WooCommerce: Hide or Rename a My Account Tab
  6. WooCommerce: How to Merge My Account Tabs
  7. WooCommerce: Allow Users to Edit Processing Orders
  8. WooCommerce: Single Product Page Redirect for Logged In Customers
  9. WooCommerce: Rename “My Account” If Logged Out @ Nav Menu
  10. WordPress: Custom Logo @ WP Login Page

Where to add this snippet?

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.

Follow @rmelogli
Category: WooCommerce Tips
Tags: Customer Login, My Account, Redirect

Post navigation

Previous post: Storefront Theme: Edit or Remove Footer Credits
Next post: WooCommerce: Update Order Field Value After a Successful Order

15 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
  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? 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 :)

Cancel reply

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

Recent Posts
  • WooCommerce: Redirect Product Category Pages
  • WooCommerce: Close Button @ WooCommerce Checkout Notices
  • WooCommerce: Related Products Custom Heading & Subheading
  • WooCommerce: Display Stock Status For External Products
  • WooCommerce: Display Product Grid @ Order Emails e.g. Related Products
About Business Bloomer

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.

Join 75,000+ Monthly Readers & 16,500+ Subscribers.

Become a Business Bloomer Supporter.

Join BloomerArmada and become an Official Business Bloomer Supporter:
easy-peasy, and lots of perks for you.
See your Benefits →
Popular Searches: Visual Hook Guides - Checkout Page - Cart Page - Single Product Page - Add to Cart - Emails - Shipping - Prices - Hosting
Latest Articles
  • WooCommerce: Redirect Product Category Pages
  • WooCommerce: Close Button @ WooCommerce Checkout Notices
  • WooCommerce: Related Products Custom Heading & Subheading
  • WooCommerce: Display Stock Status For External Products
  • WooCommerce: Display Product Grid @ Order Emails e.g. Related Products
Latest Comments
  • Rodolfo Melogli on The Ultimate Guide to WooCommerce Coupons
  • Rodolfo Melogli on Account Management & Privacy
  • Gary
    PRIORITY COMMENTER »
    on CustomizeWoo FREE
  • IAN
    PRIORITY COMMENTER »
    on Account Management & Privacy
Find Out More
  • Become a WooCommerce Expert
  • WooCommerce Blog
  • WooCommerce Online Courses
  • WooCommerce Weekly
  • Bloomer Armada
  • Affiliate Program
  • 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:

Email: [email protected]

Twitter: @rmelogli

Hire me by the hour: Get Quote »

VisaMastercardAmexPayPal Acceptance Mark
Business Bloomer © 2011-2023 - Terms of Use - Privacy Policy