Are you looking for a solution for WooCommerce logout without confirmation? WooCommerce users management requires that you have a way of adding the logout for your customers on the menu or anywhere that is visible in the frontend. If you are a WooCommerce theme developer or a shop owner you probably have comes across this message on the logout function “Are you sure you want to log out?” if this bothers you and your customers I will show you how to easily and quickly remove it in less than five minutes.
Implement WooCommerce Logout Without Confirmation
One of the most annoying problems with WooCommerce logout process is this persistent logout message “Are you sure you want to log out? “One of my most common requests from WooCommerce development clients is to remove this message and make the logout process to be automatic or smooth.
I want to share with you two ways you can remove this logout confirmation message and automatically logout your customers.
Method One
To bypass this WooCommerce logout confirmation, there is a hook you can use.
Open your functions.php file in your theme and add the code:
add_action('check_admin_referer', 'logout_without_confirm', 10, 2); function logout_without_confirm($action, $result) { /** * Allow log out without confirmation */ if ($action == "log-out" && !isset($_GET['_wpnonce'])) { $redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : ''; $location = str_replace('&', '&', wp_logout_url($redirect_to));; header("Location: $location"); die(); }
This code simply checks if the action parameter passed to the function is equal to the logout and it the Nonce is set for security purposes. If these two conditions are met we logout and redirect to the specified location.
Remove WooCommerce Logout Confirmation with Template Redirect
Another alternative method that we can use is using the Global $wp object and the template_redirect action.
Method Two
The following is the code that you should add to the functions.php file of your theme to remove the WooCommerce logout confirmation message:
add_action( 'template_redirect', 'logout_confirmation' ); function logout_confirmation() { global $wp; if ( isset( $wp->query_vars['customer-logout'] ) ) { wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) ); exit; } }
Using either of the two code snippets you can easily get rid of the WooCommerce logout confirmation message and allow your users to have a smooth logout process and excellent user experience.
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.
More articles written by Joe
Comments are closed.