Display orders instead of woocommerce my account dashboard for logged in users [closed]

There are /login /register /lost-password /reset-password pages with content:
[woocommerce_my_account].

After successful registration or login, the user is redirected thanks to:

function redirect_to_orders_after_login( $redirect, $user ) {  
    $redirect=site_url('/my-account/orders/'));
    return $redirect ;
}

add_filter( 'woocommerce_login_redirect', 'redirect_to_orders_after_login', 10, 2 );

Dashboard menu item is hidden thanks to the below code:

function custom_my_account_menu_items( $items ) {
  $items = array(
      'orders'    => __( 'Orders', 'woocommerce' ),
      'edit-account'      => __( 'Edit account details', 'woocommerce' )
  );
  return $items;
}

add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );

But if site_url/login and other above urls are accessed by a logged in users, my account dashboard is still displayed.

I tried to use the following code, but don’t believe it covers all the cases, because accessing site_url/my-account/lost-password still redirects to the dashboard:

add_action('init', function() {
  $url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), "https://wordpress.stackexchange.com/");
  if ( is_user_logged_in() && ($url_path == 'login' | $url_path == 'register' | $url_path == 'lost-password' | $url_path == 'reset-password') ) {     
     wp_redirect( site_url('/my-account/orders/') );
     exit(); 
  }
});

Maybe, overriding dashboard.php with the code in orders.php in the child theme is the solution?

How to redirect a user to my-account/orders correctly and hide the dashboard so it won’t be accessible and viewable at all?

1 Answer
1

How to redirect a user to my-account/orders correctly

Try this:

add_action( 'parse_request', 'redirect_to_my_account_orders' );
function redirect_to_my_account_orders( $wp ) {
    // All other endpoints such as change-password will redirect to
    // my-account/orders
    $allowed_endpoints = [ 'orders', 'edit-account', 'customer-logout' ];

    if (
        is_user_logged_in() &&
        preg_match( '%^my\-account(?:/([^/]+)|)/?$%', $wp->request, $m ) &&
        ( empty( $m[1] ) || ! in_array( $m[1], $allowed_endpoints ) )
    ) {
        wp_redirect( '/my-account/orders/' );
        exit;
    }
}

Leave a Comment