WooCommerce Redirect After Logout

WooCommerce Redirect After Logout

Woocommerce does not give the custom option for redirecting your customers after logout. Many times, we need to redirect the users to specific pages like Home page or any other pages maybe a custom login form.

Here, we will talk about how to redirect the user after they use the logout option on the My Account page in WooCommerce.

Redirect to Homepage after Logout Snippet

just add this Snippet in function.php file of your child theme

*WordPress Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit.

<?php
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout()
{
	wp_redirect( home_url() );
	exit();
}
?>

You can also add an HTTP response status code, such as 301 for a “Moved Permanently” as a second parameter to wp_redirect() if you ever needed.

<?php
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout()
{
	wp_redirect( home_url(), 301 );
	exit();
}
?>

WooCommerce Redirect After Logout by Page (By ID)

The page can also be added to the WooCommerce logout redirect function using the page id. You need to know the ID of the page where you want to redirect the user so that you can pass it in the redirect function.

<?php
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout()
{
	wp_redirect( get_permalink(32) );
	exit();
}
?>

WooCommerce Redirect After Login

<?php
add_filter('woocommerce_login_redirect', 'auto_redirect_after_login');
function auto_redirect_after_login($redirect_to) 
{
	return home_url();
}
?>

Performs a safe (local) redirect, using wp_redirect().

wp_redirect() does not validate the given url if it is a reference to the current host or not. That means that this function is vulnerable to open redirects if you pass it a url supplied by the user. For this reason, it is best to always use wp_safe_redirect() instead, because it will ensure that the url refers to the current host only. Only use wp_redirect() when you are specifically trying to redirect to another site, and then you can hard-code the URL.

<?php
add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout()
{
	wp_safe_redirect( $url );
	exit();
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

The link has been Copied to clipboard!