user6349891
user6349891

Reputation:

Woocommerce custom logout without confirmation

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/");

Upvotes: 3

Views: 15994

Answers (5)

user728290
user728290

Reputation: 1

You can use this if you prefer a logout shortcode tag for html (with optional redirect slug between tags):

/* Shortcode for no confirmation logout link with optional redirect slug between shortcode tags */
//
//  E.g. [logout_link]my-account[/logout_link]
//
function logout_to_optional_redirect_slug_function( $atts, $redirect_slug = null ) {

    $redirect_full_url = get_site_url(null, '/', 'https') . $redirect_slug;
    $logout_url = wp_logout_url($redirect_full_url);
    $logout_hyperlink = "<a href='".$logout_url."'>Logout</a>";

    return do_shortcode($logout_hyperlink);
}
add_shortcode( 'logout_link', 'logout_to_optional_redirect_slug_function' );

Upvotes: 0

Huynh Thai Hung
Huynh Thai Hung

Reputation: 91

You can create new shortcode log out URL in function.php of theme and add everywhere you want in tag <a href="[custom_logout_s]"></a> html. I tried and success.

// Custom shortcode log out
function custom_logout()
{
    return wp_logout_url(bloginfo( 'url' ));
}
add_shortcode( 'custom_logout_s', 'custom_logout' );

Upvotes: 0

Serge Goncharov
Serge Goncharov

Reputation: 51

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.

Upvotes: 5

nicolas181070
nicolas181070

Reputation: 21

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

Upvotes: 2

chidambara selvan
chidambara selvan

Reputation: 141

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

Upvotes: 4

Related Questions