WooCommerce Logout without Confirmation : How to Remove “Are you sure you want to log out?”

Bypass logout confirmation in WooCommerce 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.

WooCommerce Logout without Confirmation: Are you sure you want to log out?"

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.

Similar Articles

Comments are closed.