Bypass “Are you sure you want to log out?” message in WooCommerce

Quick Summary

  • A template_redirect callback detects the customer-logout endpoint and redirects to wp_logout_url(), which includes a valid nonce, so the confirmation never shows.
  • After logout, customers land on the My Account page from wc_get_page_permalink(); pass another URL, such as home_url(), to wp_logout_url() to change it.
  • With the confirmation gone, any visit to the logout URL logs the user out, including from links placed on other sites.

WooCommerce shows the message “Are you sure you want to log out?” with a confirmation link when a customer opens the logout endpoint (customer-logout by default) without a valid security nonce. That’s common when you add a plain Logout link to a menu. The snippet below skips the confirmation and logs the customer out right away.

Add it to your child theme’s functions.php or a code snippets plugin.

PHP
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;
    }
}

How it works

  • The function runs on template_redirect, before WordPress loads the page template. It’s the same hook you’d use to block direct access to a page.
  • $wp->query_vars['customer-logout'] is set when the current URL is WooCommerce’s logout endpoint.
  • wp_logout_url() builds a logout link that includes a valid nonce, with the My Account page from wc_get_page_permalink( 'myaccount' ) as the redirect target. After logging out, the customer lands on the My Account login form.
  • wp_logout_url() separates the URL parameters with &, which is meant for HTML output. str_replace() turns them back into & so the redirect works.
  • exit stops WordPress from rendering the confirmation page after the redirect.

To send customers somewhere else after logout, pass a different URL to wp_logout_url(), for example home_url().

Keep in mind that the confirmation protects customers from being logged out by a link on another site. With this code, any visit to the logout URL logs the user out. It’s also a good idea to give logout_confirmation your own prefix, so it can’t clash with another function of the same name.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More