Home » WooCommerce: Login Redirect by User Role @ My Account

WooCommerce: Login Redirect by User Role @ My Account

by Tutor Aspire

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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */

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

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

You may also like