Recently, I had to work on a site that had a few things going on:
- It had some users with custom roles, and capabilities
- Those users had a simplified dashboard
- WooCommerce was enabled.
The problem was that when those users logged in, WooCommerce would redirect them to the “My Account” page, instead of their simplified dashboard.
I found a lot of places where they showed how to redirect users after login to “any” page, but it didn’t seem to work when the dashboard was that page.
After some digging around, I discovered that WooCommerce also redirects users that try to access the Dashboard directly to “My Account”.
So, if you are ever in this situation you need to do two things:
- Redirect the login (and registration) to the Dashboard
- Allow them to access the Dashboard
Here’s the code (change “custom_role” to your users’ role):
// After login or registration, redirect users with "Custom Role" to the Dashboard instead of WooCommerce's My Account
function mytheme_login_redirect($redirect) {
if (current_user_can('custom_role') || current_user_can('administrator')) {
$user = get_current_user_id();
$redirect = get_dashboard_url($user);
}
return $redirect;
}
add_filter('woocommerce_login_redirect', 'mytheme_login_redirect');
add_filter('woocommerce_registration_redirect', 'mytheme_login_redirect');
// Allow users that have "Custom Role" to view the Dashboard
function mytheme_woocommerce_admin($access) {
if ( current_user_can('custom_role') || current_user_can('administrator')) {
$access = false;
}
return $access;
}
add_filter( 'woocommerce_prevent_admin_access', 'mytheme_woocommerce_admin' );
Hope this helps someone.