简体   繁体   中英

WooCommerce - My Account - logout and redirect to Homepage

一旦用户进入他们在 WooCommerce 上的“我的帐户”页面,我希望他们退出并重定向到我的主页,而不是最终进入登录页面。

add_action('wp_logout','auto_redirect_after_logout');

function auto_redirect_after_logout(){

  wp_redirect( home_url() );
  exit();

}

If you find how to bypass WooCommerce logout confirmation, you can take this :

function wooHook_woocommerce_logout_bypass() {
    global $wp;

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

add_action( 'template_redirect', 'wooHook_woocommerce_logout_bypass' );

Just to add to the original answer, using exit to terminate script can stop Woocommerce from executing necessary after logout like clearing the cookies (cart items in cookies). So your best bet is using logout_url filter like this.

function redirect_after_logout($logout_url, $redirect) {
    return $logout_url . '&redirect_to=' . home_url();
}
add_filter('logout_url', 'redirect_after_logout', 10, 2);

Tested in Woocommerce Version 5.4.1 ✅

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM