Woocommerce custom logout without confirmation

Viewed 11407

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

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( '&', '&', wp_logout_url( wc_get_page_permalink( 'myaccount' ) ) ) );
        exit;
    }
}

add_action( 'template_redirect', 'iconic_bypass_logout_confirmation' );

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

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