Output the logout link with wc_logout_url() and it will be nonced and have no confirmation. The confirmation is a security measure.
But I’m using one of the Woocommerce endpoints to show Logout link.
SCREEN
I’m not inserting link like that:
<a href="<?php echo wp_logout_url(); ?>">Logout</a>
So maybe there is some filter/function to replace native Woocommerce endpoint behavior?
I am adding a logout link the same way as @jurasjo and I would also like to know how to remove the logout confirmation. I am using WooCommerce endpoints in a menu.
There has to be some sort of function that would add the WP nonce to the WooCommerce /customer-logout/ endpoint. Having users confirm their logout is not very user-friendly.
Hi @jurasjo,
I found a workaround, but I can’t get it to work with the WooCommerce endpoint. At least the users won’t have to confirm the logout.
If you create a custom link in your menu, set the label to “Logout”, and set the URL to http://yourdomain.com/wp-login.php?action=logout. Then add this function to your functions.php file:
function change_menu($items){
foreach($items as $item){
if( $item->title == "Logout"){
$item->url = $item->url . "&_wpnonce=" . wp_create_nonce( 'log-out' );
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'change_menu');
I am still trying to figure out how to redirect users after logout, because I don’t want them to see the default WP login page after they logout. Also, I’d still like the WooCommerce endpoint to include the wpnonce.
This should work:
// Logout without 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' );
Found it here: https://iconicwp.com/bypass-are-you-sure-you-want-to-log-out-message-in-woocommerce/
@jurasjo ‘s code didnt work for me but @caleb ‘s did.
To redirect I just added “&redirect_to=logout”
“logout” is the name of the page (the permalink/url) I wanted to redirect to in my case but you can use anything in there.
Final URL that I added to the menu via Custom Link with “Logout” label was:
http://yourdomain.com/wp-login.php?action=logout&redirect_to=logout
With this code:
function change_menu($items){
foreach($items as $item){
if( $item->title == "Logout"){
$item->url = $item->url . "&_wpnonce=" . wp_create_nonce( 'log-out' );
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'change_menu');