WooCommerce Login Redirect Hook Explained with Example

woocommerce login redirect hookWooCommerce login redirect hook allows users to create redirect after login in WooCommerce. If you are trying to create a code solution for WooCommerce redirect after customer login, you need to understand how the login redirect hook works in WooCommerce.

In this quick tutorial, I will show you what you need to know about the WooCommerce login redirect hook and how to use it to create redirect for your customers after they login.

If you would like to create other WooCommerce redirects, you should consider checking out my guides on WooCommerce redirect after checkout and WooCommerce redirect after logout.

WooCommerce Login Redirect Hook

WooCommerce can be customized using hooks and each hook play an important role in creating custom options. To create a redirect after the customer log in, you need the woocommerce_login_redirect filter hook coupled with a callback function with the destination of your redirect.

WooCommerce redirect after login hook is a filter hook that can be applied to create a redirect after login as in the code below :

add_filter( 'woocommerce_login_redirect', 'njengah_woocommerce_redirect_after_login', 9999, 2 );
 
function njengah_woocommerce_redirect_after_login( $redirect, $user ) {
     
        $redirect = get_home_url(); // homepage
        //$redirect = wc_get_page_permalink( 'shop' ); // shop page
        //$redirect = '/custom_url'; // custom URL same site
        //$redirect = 'https://example.com'; // External url
        //$redirect = add_query_arg( 'password-reset', 'true', wc_get_page_permalink( 'myaccount' ) ); // custom My Account tab
    
  
    return $redirect;
}

This code creates a redirect after login to the homepage and you can change the code to fit any other page you wish to redirect customers after they login.

Filter Hook : woocommerce_login_redirect

As you have seen on the example above, you can use this filter hook just like any other filter, the callback function takes two parameters as I will explain below:

Parameter Description
$redirect This is the redirect destination it should be a string
$user This is the user to redirect and this is obviously an object

The basic usage of this filter is as in the code sample below :

         
// add the filter 
add_filter( 'woocommerce_login_redirect', 'filter_woocommerce_login_redirect', 10, 2 );

// define the woocommerce_login_redirect callback 
function filter_woocommerce_login_redirect( $redirect, $user ) { 
    // make filter magic happen here... 
    return $redirect; 
};

Remove WooCommerce Login Redirect Hook

Its is possible to reverse the WooCommerce login redirect hook by using remove_filter function to remove an existing redirect after login in a WooCommerce store. The following is the simple code to help in removing the WooCommerce login redirect hook.

// remove the filter
remove_filter( 'woocommerce_login_redirect', 'filter_woocommerce_login_redirect', 10, 2 );

Conclusion

In this post, we have highlighted the woocommerce redirect after login hook that you can use to create the redirect for customers after login. I have demonstrated how you can use the hook parameters to create more diverse redirect options after login. Finally, I have shown you how to remove the redirect after login hook if you wish to reverse the option.

Similar Articles