Account Contact Search
Business Bloomer
  • WooCommerce Tips
  • WooCommerce Plugins
  • WooCommerce Courses
  • More
    • WooCommerce Dev & Consultation: get a quote for WooCommerce customization & troubleshooting
    • Private Community for WooCommerce Developers: get WooCommerce support, perks and discounts
    • Free WooCommerce Tips Newsletter: get tutorials, snippets and guides directly into your inbox
    • Ecommerce Book: a simple book for entrepreneurs who are struggling with online sales
    • Guest Blog / Co-Marketing: gain exposure by tapping into Business Bloomer audience
    • WooCommerce Website Audit Service: get a conversion / performance optimization audit
    • WooCommerce Mini-Plugin Support: create a support ticket for your plugin purchase
  • 0
  • Home
  • WooCommerce Blog
  • WooCommerce Plugins: If you don’t feel confident with PHP and JS coding, and need more control over your WooCommerce customization with additional settings and functionalities, we’ve developed a series of mini WooCommerce plugins. You don’t want to purchase yet another bloated, expensive plugin? Great! Because Business Bloomer mini WooCommerce plugins come without the usual hassles. One feature. Lifetime license. No annoying subscriptions. 1 plugin file. A few lines of code. No banners. No up-sells. No WP notifications. Use it on as many websites as you like. Lifetime support. 1-page documentation. A single and easy admin dashboard.
  • WooCommerce Online Courses
  • Account
  • Search
  • Contact
  • Cart

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: 26 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:

  • WooCommerce: Login Redirect to Previous URL @ My Account We’ve already seen how to set a custom My Account login redirect URL by user role – but today we...
  • WooCommerce: Custom Login Redirect @ My Account The WooCommerce My Account login form redirects to… itself once a WordPress user logs in. So, what if you want...
  • WooCommerce: Change User Role for New Customers If you don’t want to assign the WooCommerce user role “customer” to new… customers, there is simple PHP that can...

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its 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 guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went 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.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 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

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? 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: Get “Trending” Products
  • WooCommerce: Order Number Prefix / Suffix
  • Cart slide-in?
  • WooCommerce order source?
  • Can WooCommerce error messages be displayed in another place?
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 & 17,000+ 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: Get “Trending” Products
  • WooCommerce: Order Number Prefix / Suffix
  • Cart slide-in?
  • WooCommerce order source?
  • Can WooCommerce error messages be displayed in another place?
Latest Comments
  • Rodolfo Melogli on WooCommerce: Hide “Private” Products From The Shop Page
  • Maria Kelley
    PRIORITY COMMENTER »
    on WooCommerce: Hide “Private” Products From The Shop Page
  • Rodolfo Melogli on Cart slide-in?
  • Rodolfo Melogli on WooCommerce order source?
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