Reserve Your Free Seat for Our Next WooCommerce Class! Search
Business Bloomer
  • Business Bloomer Club
  • 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 User Roles

WooCommerce: Login Redirect by User Role @ My Account

Last Revised: Sep 2022

STAY UPDATED

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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;
}
 

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: Disable Payment Gateway for Specific User Role
    You may want to disable payment gateways depending on the logged in user role. For example, you may want to disable PayPal for user role…
  • 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…

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

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

  1. Arydiel
    June 17, 2023

    Still working. Thanks! 🙂

    Reply
    1. Rodolfo Melogli
      June 22, 2023

      Great!

      Reply
  2. 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
  3. Shekhor Sarker
    July 11, 2022

    Perfectly Working.. Awesome Thanks..

    Reply
    1. Rodolfo Melogli
      July 18, 2022

      Great!

      Reply
  4. 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
  5. 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
  6. Victor
    February 14, 2021

    Helpful. Thank you.

    Reply
    1. Rodolfo Melogli
      February 18, 2021

      Cool

      Reply
  7. 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
  8. hidenobu miyata
    December 16, 2020

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

    Reply
    1. Rodolfo Melogli
      December 16, 2020

      Great!

      Reply
  9. 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
  10. 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
  11. 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
  12. 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? 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: How to Configure Product and Order Sync
  • WooCommerce: “Beautify” Item Meta in Order Emails
  • WooCommerce: Per-Product Checkout Fees / Tariffs
  • WooCommerce: Auto-Cancel Orders After 3 Failed Payments
  • WooCommerce: Bulk Delete Pending / Failed Scheduled Actions

Latest Comments

  1. Rodolfo Melogli on WooCommerce: Remove “Payments” From WordPress Sidebar Admin Menu
  2. Rodolfo Melogli on WooCommerce: Remove “Payments” From WordPress Sidebar Admin Menu
  3. Rodolfo Melogli on WooCommerce: Remove “Payments” From WordPress Sidebar Admin Menu

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