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.

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.

21 Comments

  1. abhi says:

    but where we need to add this code?

  2. Corey Rubadue says:

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

    Thanks!

  3. Gemma Wild says:

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

  4. robert_viruss says:

    nice one 😉

  5. Magic Filou says:

    Hi!

    Your code works fine but I’m trying to redirect to a specific page by lang if I put
    wp_logout_url( wc_get_page_permalink( ‘mygoodbyepage’ ) … doesn’t work … it send me to the home page
    if I try to put :
    wp_logout_url( https://www.mydomain.com/my… ) doesn’t work because I need to add ‘ ‘ around my url … BUT ^^
    I’m using wpml so I wish to add my language attribute to send my visitor on a specific url.
    My url structure is http://www.mydomain.com US version http://www.mydomain.com/es/ Spanish version … etc …
    So I wish to send my spanish visitor to http://www.mydomain.com/es/mygoodb...
    my french visitor to http://www.mydomain.com/fr/mygoodb...
    my english visitor to http://www.mydomain.com/mygoodbyep... (main language) ….for now all language send to this page 🙁

    FYI the customer-logout page is under the same structure
    .com/my-account/customer-logout/ US
    .com/es/my-account/customer-logout/ ES
    .com/fr/my-account/customer-logout/ FR

    For now .. I don’t achieve this goal 🙁 .. If you have any tip 😉
    Actually using your code without any modification works fine for all languages, every logout send to the home page in the good language … but I wish to send my traffic to a special good bye page by lang 🙂
    Sorry for my poor english ^^ , TIA
    Cheers
    Phil
    ….

    • James Kemp says:

      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.

      • Magic Filou says:

        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 🙁

        • Magic Filou says:

          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’ ) )));

          • Magic Filou says:

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

          • Magic Filou says:

            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 :-/

    • Magic Filou says:

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

  6. Jeremy says:

    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?

  7. Ozgur says:

    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();

  8. Isabel says:

    This works perfectly fine.

  9. Olga says:

    Works great! Thanks

  10. You made my day! thanks a lot

  11. Bhargava Prabu Reddy says:

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

  12. Tang says:

    It works for me. Great. Thanks a lot.

  13. Riz says:

    Thanks helped a lot!!

  14. Danielle says:

    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

Leave a Reply

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