3

Once a user is in their My Account page on WooCommerce I'd like them to hit logout and be redirected to my Homepage rather than end up on the login page.

mujuonly
  • 11,370
  • 5
  • 45
  • 75
Rich72
  • 63
  • 1
  • 1
  • 4

3 Answers3

12
add_action('wp_logout','auto_redirect_after_logout');

function auto_redirect_after_logout(){

  wp_redirect( home_url() );
  exit();

}
mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • I've added this into my function.php file. When I clicked to access My Account the login page did not fire. Nothing fired in fact. Is it not as simple as adding this code into the function.php file? – Rich72 Feb 26 '19 at 11:26
  • I added the code into my child theme. https://apexgray.co.uk/my-account/ – Rich72 Feb 26 '19 at 11:30
  • I'll add the code back into the function.php file again and let you know... – Rich72 Feb 26 '19 at 11:32
  • Maybe my question wasn't clear. When the user has logged into their account and they've finished, when they hit logout I ideally would like them to go to the home page. – Rich72 Feb 26 '19 at 11:37
  • I think my original question was confusing...! – Rich72 Feb 26 '19 at 11:38
  • works a charm Mujeeba! Thank you. Wish I knew how to do this! – Rich72 Feb 26 '19 at 11:55
1

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_urlfilter 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 ✅

-1

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' );
SherylHohman
  • 16,580
  • 17
  • 88
  • 94