Woocommerce 自定义注销无需确认

Woocommerce custom logout without confirmation

我正在尝试在 woocommerce 中实现无需注销确认的自定义注销。

我创建了一个页面并添加了这段代码。并将此页面的 link 添加到菜单。但是它不起作用。

 session_start();
 session_destroy();   
 header("Location:http://www.liberatium.com/");

确认发生是因为您在 URL 中缺少必要的随机数,正在 wp-login.php

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

使用 wp_logout_url 以检索包含随机数的 URL。如果您想重定向到自定义 URL,只需将其作为参数传递即可。

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

如果这不起作用,请在 functions.php 中添加以下函数并尝试上面的代码...

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

我在 wordpress functions.php 中使用了这段代码,用于在付款后注销用户或关闭浏览器

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

我在菜单项中使用 woocommerce endpoing 注销 link like

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

&customer-logout=true - 此处的关键点是无需确认即可注销。只需将其添加到菜单项中 link 的 and 即可。

此方法的一个缺点 - 成功注销后,用户将重定向到登录页面,而不是当前页面。

您可以在主题 function.php 中创建新的简码注销 URL 并在标签 <a href="[custom_logout_s]"></a> html 中添加任何您想要的地方。我试过了,成功了。

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

如果您更喜欢 html 的注销短代码标签(标签之间有可选的重定向 slug),您可以使用它:

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