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!
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;
}
Still working. Thanks! 🙂
Great!
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
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
Perfectly Working.. Awesome Thanks..
Great!
Doesn’t seem to be working anymore.
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?
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?
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!
Helpful. Thank you.
Cool
why it not working for me? still the login function redirect to my account dashboard
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!
Thank you so much, You’ve been very helpful.
Great!
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
Yes sorry, possible but it’s custom work
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?
Not a multisite expert, sorry
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.
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!
Great snippet Rodolfo – how can I adjust to include a custom role like ‘trade’ as well as ‘customer’
Thank you Amanda! Just add another wc_user_has_role() call with the OR PHP operator
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!