Woocommerce custom logout without confirmation

10,117

Solution 1

Confirmation happens because you are missing the neccessary nonce in the URL, which is being checked in wp-login.php

case 'logout' :
check_admin_referer('log-out');
...

Use wp_logout_url in order to retreive the URL including the nonce. If you want to redirect to a custom URL, simply pass it as an argument.

<a href="<?php echo wp_logout_url('http://www.liberatium.com/') ?>">Log out</a>

if that's not working means, Add below function in functions.php and try above code...

add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
   function logout_without_confirm($action, $result)
      {
      /**
      * Allow logout without confirmation
      */
      if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
      $redirect_to = isset($_REQUEST['redirect_to']) ? 
      $_REQUEST['redirect_to'] : '';
      $location = str_replace('&amp;', '&', wp_logout_url($redirect_to));;
      header("Location: $location");
      die();
    }
}

Solution 2

I used this code in the wordpress functions.php, to logout user after payment or close the browser

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

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

add_action( 'template_redirect', 'iconic_bypass_logout_confirmation' );

Solution 3

I use the woocommerce endpoing logout in the menu item with link like

https://yoursite/my-account/customer-logout/?_wpnonce=2bbbac43a8&customer-logout=true

&customer-logout=true - is keypoint here to logout without confirmation. Just add it to the and of link in menu item.

One disadvantage of this method - after successfull logout user is redirecting to login page, not to the current page.

Share:
10,117

Related videos on Youtube

Admin
Author by

Admin

Updated on June 25, 2022

Comments

  • Admin
    Admin about 2 years

    I am trying to implement custom logout without logout confirmation in woocommerce.

    I created a page and added this code. and added link of this page to menu. But its not working.

     session_start();
     session_destroy();   
     header("Location:http://www.liberatium.com/");
    
  • Admin
    Admin almost 7 years
    Thank you so much.
  • user728290
    user728290 about 4 years
    I needed a shortcode to add the logout hyperlink as a menu item to my menubar. To add shortcodes in menu items, I use the plugin "Shortcode in Menus" by Gagan Deep Singh.