WooCommerce 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.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Insert Default Content to WordPress Post Editor Dynamically
- How Add Text Before the Price in WooCommerce » Add Text Before Price
- How to Add Search Box on Top of Page Storefront Theme
- How to Change Out Of Stock Text in WooCommerce
- How to Redirect Users After Registration by Roles In WooCommerce
- How to Change Return to Shop Link in Woocommerce
- How to Redirect User If Not Logged in WordPress » Page Redirect
- How to Redirect to Cart after Login in WooCommerce
- How to Simplify Checkout Free Items Storefront Theme
- How to Logout without Confirmation WooCommerce : How to Remove “Are you sure you want to log out?”
- How to Setup WooCommerce Storefront Blog
- How to Use WordPress Custom Hooks do_action & apply_filters With Examples
- How to Change Add to Cart Text WooCommerce Guide with Examples
- How to Add WooCommerce Storefront Hero Image
- How to Move Primary Menu Storefront WooCommerce
- How to Create My Account Page In WooCommerce
- How to Add New Menu in My Account Page Menu in Woocommerce
- How to Access WordPress Admin Dashboard or Login to your WordPress Dashboard