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

WooCommerce: Login Redirect by User Role @ My Account

> Published: May 2020 - Revised: Sep 2022
> Blog Category: WooCommerce Tips
> Blog Tags: Customer Login, My Account, Redirect, User Roles
> Blog Comments: 24 Comments
Tweet

Join 17,000+ WooWeekly subscribers

There are times when you don’t want customers to login and be redirected to the default “My Account” dashboard. Maybe because you have a membership site and you want them to go to a custom “Welcome” page, or maybe you wish to send them straight to their “My Account” > “Downloads” subsection.

No matter the why, for sure figuring out how to achieve a custom redirect once a user logs in from the “My Account” page is quite straightforward. The hook we’ll use is called “woocommerce_login_redirect” and allows us to trigger a safe redirect whenever a customer clicks on the LOGIN button. Enjoy!

Our goal is to redirect a customer to a different URL than the “My Account” page. The snippet below will just do what it says on the tin.

PHP Snippet: On Customer Login, Redirect to Custom URL (as opposed to “My Account”)

By default, the process_login() WooCommerce function redirects to wc_get_page_permalink( ‘myaccount’ ), which is the My Account dashboard page URL. In the examples below we will try to redirect customers (and NOT administrators or other user roles) to a custom URL.

I will give you different redirect options inside the snippet, please make sure you choose only one and delete/comment out the others.

/**
 * @snippet       Custom Redirect @ WooCommerce My Account Login
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'woocommerce_login_redirect', 'bbloomer_customer_login_redirect', 9999, 2 );

function bbloomer_customer_login_redirect( $redirect, $user ) {
    
    if ( wc_user_has_role( $user, 'customer' ) ) {
        $redirect = get_home_url(); // homepage
        //$redirect = wc_get_page_permalink( 'shop' ); // shop page
        //$redirect = '/custom_url'; // custom URL same site
        //$redirect = 'https://custom.url'; // custom URL other site
        //$redirect = add_query_arg( 'password-reset', 'true', wc_get_page_permalink( 'myaccount' ) ); // custom My Account tab
    }
 
    return $redirect;
}
 

Related posts:

  1. WooCommerce: Login Redirect to Previous URL @ My Account
  2. WooCommerce: Custom Login Redirect @ My Account
  3. WooCommerce: Change User Role for New Customers
  4. WooCommerce: Separate Login, Registration, My Account Pages
  5. WooCommerce: Redirect My Account Tab to URL
  6. WooCommerce: Custom Registration Redirect @ My Account
  7. WooCommerce: Disable Payment Gateway for Specific User Role
  8. WooCommerce: Hide or Rename a My Account Tab
  9. WooCommerce: How to Merge My Account Tabs
  10. WooCommerce: Hide Specific Orders (e.g. On Hold) From My Account Orders 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, User Roles

Post navigation

Previous post: WooCommerce: Hide Category Images @ Shop
Next post: WooCommerce: 6 Tips to Improve User Experience

24 thoughts on “WooCommerce: Login Redirect by User Role @ My Account”

  1. John
    December 11, 2022
    add_filter('woocommerce_login_redirect', 'bbloomer_customer_login_redirect', 9999, 2 );

    causes a syntax error.

    [11-Dec-2022 04:27:30 UTC] PHP Parse error: syntax error, unexpected ‘add_filter’ (T_STRING) in /home2/XXXX/public_html/wp-content/themes/divi_child/functions.php on line 19
    Thanks for making this available

    Reply
    1. Rodolfo Melogli
      December 16, 2022

      Don’t know what’s before add_filter, you may have forgotten a closing bracket. Or check the single quotes, copy and paste sometimes it’s messed up. Let me know

      Reply
  2. Shekhor Sarker
    July 11, 2022

    Perfectly Working.. Awesome Thanks..

    Reply
    1. Rodolfo Melogli
      July 18, 2022

      Great!

      Reply
  3. Lewis
    September 3, 2021

    Doesn’t seem to be working anymore.

    Reply
    1. Rodolfo Melogli
      September 15, 2021

      Hi Lewis, did it work before for you? Code is fine and should work, so it could be something else. What code did you use?

      Reply
  4. Koen
    February 15, 2021

    the code redirects you, but if you push on the back button of the browser I am logged in with that user and role.
    how can I prevent the user to be logged in?

    Reply
    1. Rodolfo Melogli
      February 19, 2021

      Hi Koen, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  5. Victor
    February 14, 2021

    Helpful. Thank you.

    Reply
    1. Rodolfo Melogli
      February 18, 2021

      Cool

      Reply
  6. shahana jassim
    January 3, 2021

    why it not working for me? still the login function redirect to my account dashboard

    Reply
    1. Rodolfo Melogli
      February 6, 2021

      Hi Jassim, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet.

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentytwenty” theme (load the snippet there in functions.php) as explained here: https://www.businessbloomer.com/lesson/trwm4l01/

      Once you do that, does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      Reply
  7. hidenobu miyata
    December 16, 2020

    Thank you so much, You’ve been very helpful.

    Reply
    1. Rodolfo Melogli
      December 16, 2020

      Great!

      Reply
  8. Dan
    November 25, 2020

    Hi, I’ve been wondering if the same can be done with registrations i.e. send new ‘customer’ role registrations to a bespoke page?

    I did try replacing instances of:

    woocommerce_login_redirect

    with

    woocommerce_registration_redirect

    but it did not work. If I search the web I can find many examples of registration redirects but they don’t seem to address redirection according to role like this snippet. I appreciate this may be custom work that is too big to be answered here but it might be a good future standalone ‘tip’!

    Thanks

    Dan

    Reply
    1. Rodolfo Melogli
      November 25, 2020

      Yes sorry, possible but it’s custom work

      Reply
  9. Anders
    August 26, 2020

    Great snippet!

    However, how can you implement this on a multisite installation? Then I would like to have to different redirects…
    For example; You’re logging in on mysite.com, and when you’re logged in I want the redirect to go to mysite.com/custom-page. But in the same multisite installation, I also have mysite.net and when a user logs into that site I want the redirect to go to mysite.net/custom-page.

    Any ideas on this?

    Reply
    1. Rodolfo Melogli
      August 28, 2020

      Not a multisite expert, sorry

      Reply
  10. ben
    August 25, 2020

    Thank you for this and all your others – they have helped me more than once!

    I have been looking and trying snippets and code from so many places, and still, am stuck on redirecting users to the previous page upon login – the previous page being protected by Woo Subscriptions. Can I modify this snippet to get that result?

    Thank you for your time.

    Reply
    1. Rodolfo Melogli
      August 28, 2020

      Hi Ben, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      Reply
  11. Amanda
    May 20, 2020

    Great snippet Rodolfo – how can I adjust to include a custom role like ‘trade’ as well as ‘customer’

    Reply
    1. Rodolfo Melogli
      May 26, 2020

      Thank you Amanda! Just add another wc_user_has_role() call with the OR PHP operator

      Reply
      1. Ian Singleton
        February 8, 2023

        How would you add the other wc_user_has_role() call , i have tried tinkering with this put can’t quite make it work. Many thanks!

        Reply
        1. Rodolfo Melogli
          February 28, 2023
          if ( wc_user_has_role( $user, 'customer' ) || wc_user_has_role( $user, 'administrator' ) ) { 
          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