How to Create WooCommerce After Login Redirect to Homepage

WooCommerce After Login Redirect to HomepageIf you want the user to be redirected to homepage after login on your WooCommerce store, this is a quick guide on how to create WooCommerce After Login Redirect to Homepage. 

I have recently explained how you can create WooCommerce redirect after checkout and also shared with you the ultimate WooCommerce logout redirect guide.

Now I want to show you how to quickly  add WooCommerce after login redirect to homepage snippet that will work seamlessly with your WooCommerce theme.

WooCommerce After Login Redirect to Homepage

To create WooCommerce redirect to homepage after logon, you need to follow the following steps  :

  1.  Open your functions.php file and ensure you create backup of this file since if you make a mistake it can lead to a white screen of death error on your WooCommerce site.
  2. Add the following code to the functions.php preferably the functions.php file of the child theme of the active theme. If you don’t know how to create a child theme, you can learn from this tutorial – how to create Storefront theme child theme.
     
    
       add_filter('woocommerce_login_redirect', 'njengah_login_redirect_to_homepage');
    
    	function njengah_login_redirect_to_homepage($redirect_to) {
    
    		return home_url();
    
    	}
    
    
    
  3. This is basically a filter hook that hooks on the woocommerce_login_redirect event and fires the callback function after the login is successful.  In the callback function we return the home_url() WordPress function that prints out the homepage URL of the current site.
  4. You can extend this logic to also redirect the user after logout using the code snippet below :
     
    
    	add_action('wp_logout','njengah_logout_redirect_to_homepage');
    
    	function njengah_logout_redirect_to_homepage(){
    
    		wp_redirect( home_url() );
    		
    		exit;
    
    	}
    
    

In this case we are using a different action hook that is the default WordPress logout hook that I explained in details in the WooCommerce redirect after logout guide.

Conclusion

In this quick tutorial, we have outlined how you can create a redirect to homepage after login in WooCommerce using a simple code snippet added to the functions.php of your child theme. If this is too complex for you to implment, you can get in touch with me for further help.

Similar Articles

Comments are closed.