Skip "Are You Sure You Want to Log Out?" for Menu Links
Adds the security token to My Account log out links placed in your navigation menus, so customers log out in one click instead of seeing a confirmation page.
Opens WPIDE on
Code
add_filter( 'wp_nav_menu_objects', function ( $items ) {
if ( ! function_exists( 'wc_get_account_endpoint_url' ) || ! is_user_logged_in() ) {
return $items;
}
$logout = untrailingslashit( wc_get_account_endpoint_url( 'customer-logout' ) );
foreach ( $items as $item ) {
if ( untrailingslashit( strtok( (string) $item->url, '?' ) ) === $logout ) {
$item->url = wc_logout_url();
}
}
return $items;
} );
Notes
Point a custom menu link at /my-account/customer-logout/ and this snippet turns it into a direct, secure log out link.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.