Tips

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

In the latest versions of WooCommerce, when a customer clicks the “logout” button in their account, they see a message:

You may find that you want to bypass this message and just log the user out immediately. To do so, all you need is a simple code snippet. Add this using the Code Snippets plugin, or your preferred method.

/**
 * Bypass logout confirmation.
 */
function iconic_bypass_logout_confirmation() {
	global $wp;

	if ( isset( $wp->query_vars['customer-logout'] ) ) {
		wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) );
		exit;
	}
}

add_action( 'template_redirect', 'iconic_bypass_logout_confirmation' );

This checks if the logout endpoint is set, and if it is, logs the user out straight away and redirects to the My Account page. You can add any URL in place of wc_get_page_permalink( 'myaccount' ) for a different redirect location.

Avatar

James is the founder of Iconic and an experienced WooCommerce plugin developer. He has been featured on numerous WordPress publications.

He is a keen photographer and currently shoots using his favourite mirrorless camera, a Fuji X-T20.

32 Responses

Avatar
Zabdiel F. 3 months ago

Gracias por la aportación, después de estar buscando que Mi cuenta, logrará cerrar la sesión, al fin lo logré con este código. Muchas gracias

Avatar
Dennis Prins 6 months ago

Thanks, but the page myaccount can be cached.
So add a refresh. See full code:

function woocommerce_bypass_logout_confirmation() {
global $wp;
if ( isset( $wp->query_vars['customer-logout'] ) ) {
wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) );
header("Refresh:0");
exit;
}
}
add_action( 'template_redirect', 'woocommerce_bypass_logout_confirmation' );

Avatar

THANKS...

Very helpful.

I made one change to your scripting:

the line:

wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) );

I changed it to redirect back to Homepage ( get_home_url() ):

wp_redirect( str_replace( '&', '&', wp_logout_url( get_home_url() ) ) );

Avatar

Words for me using,
Woocommerce V4.2.0
Wordpress Version 5.4.1

If it helps, go to:
Appearance -> Theme Editor -> on the right side, select functions.php

You should see your Child Theme functions.php content.
There is a line that says,

/* Add your own functions below this line.
======================================== */

copy paste that code as it`s given here, into the space below that line.
(and click Save :)

Avatar
Amar Verma 1 year ago

I tried all solution mentioned above for my blog https://amarverm.com

but none of the solution worked out for me. :(

please share tips to resolved this issues.

Avatar
onionwhoiam 1 year ago

This had been driving me barmy. Code works great!

Handy tip - I've noticed that if you inspect the index of the 'logout' link from the user dashboard you can copy the url and use it for a custom link or endpoint for additional menu logout options (y)

Cheers

Avatar
Clappert 1 year ago

Thanks for this - however, it still uses the /wp-login.php as follows;

wp-login.php?action=logout&redirect_to=https%3A%2F%mysite.com%2Fshop%2Fmy-account%2F&_wpnonce=f5gty78

How to avoid wp-login page altogether (I have it blocked in htaccess)

Avatar

Danielle,
Go to,
Word Press Dashboard -> Appearance -> Theme Editor.

On the right side, at the top, you'll see a "Select theme to edit" menu.
Select (Your Theme) Child.

Under the menu you'll see "Theme Files". It should show style.css and functions.php (those 2 files should be in your Child theme. If they're not there just copy them from the main theme.)

Select functions.php.

In the main window, just paste the whole code at the bottom, then click Update File. If it updates correctly and there are no errors, it will says, "File updated successfully."

That's it.

Avatar
Danielle 2 years ago

Hi,

I have a child theme set up, however, i am unable to find the woocommerce threads in here.

Could you please tell me where exactly i need to add this code? Is it in the functions.php of my child theme? Also, do i need to remove anything else out of the current code?

I look forward to your reply.

Danielle

Avatar
Bhargava Prabu Reddy 2 years ago

Thanks a lot!!! It solved my problem in wordpress.

Avatar

This function wasn't enough for me for proper logout so I added these lines below to make sure user logs out

wp_destroy_current_session();
wp_clear_auth_cookie();

Avatar

Thanks worked great!

But what if I want to stay on the current page? For instance, client logs out on cart page and stay at the cart page. Or if client is on a specific category page, it will still be on that category page after logout?

Avatar
Magic Filou 3 years ago

Hi! Anybody have another solution for me to reach my goal ? :)

Avatar
James Kemp admin 3 years ago

Hey, you could try this:

wp_logout_url( get_permalink( 10 ) );

Where 10 is your primary goodbye page ID. WPML should get the ID for translated pages automatically based on the language.

Avatar
Magic Filou 3 years ago

Hi James
Thanks for this tip but I tried with this code . :
wp_redirect( str_replace( '&', '&', wp_logout_url( get_permalink( 5261 ) )));
And it still sending me to my goodbye page from my spanish logout link :(

Avatar
Magic Filou 3 years ago

neither :
wp_redirect( str_replace( '&', '&', wp_logout_url( get_permalink( '5261' ) )));
wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_permalink( '5261' ) )));
wp_redirect( str_replace( '&', '&', wp_logout_url( wc_get_page_permalink( '5261' ) )));

Avatar
Magic Filou 3 years ago

neither :
wp_redirect( str_replace( '&', '&', wp_logout_url( get_page_link( '5261' ) )));

Avatar
Magic Filou 3 years ago

This one :
wp_redirect( str_replace( '&', '&', wp_logout_url( get_permalink( get_page_by_title( 'mygoodbyepage' ) ) ))); send to the good page but in main language :-/

Avatar
Gemma Wild 3 years ago

I've been tearing my hair out trying to solving this one, hehe! Thanks so much for posting, works perfectly for me over at www.rustictaghouse.co.uk

Avatar
Corey Rubadue 4 years ago

Hi there, it seems if I put the logout link in as a submenu I still get the dashboard pop-up. Any thoughts?

Thanks!

Leave a Reply

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