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.
Hey,
i’m trying to make this code work..
Should i replace mytheme_ by the name of my theme? sorry i’m really noob with coding
Thanks
Sorry for the late reply. The code will work whether you change that part or not, but it’s better to change that for the name of your theme.
Hello and thank you for your post. I just tried using this code to resolve this same issue at my WordPress site. I could not get it to work. Am I correct in assuming this should go into the functions.php file of my theme?
Yes, it goes into functions.php. Please try again, I just spotted and fixed a typo on line 3. Also, make sure to change ‘custom_role’ on lines 3 and 17 to the name of the role you want to make the dashboard available to. Hopefully, that will fix it, but keep in mind that I posted this in March, so things may have changed on WooCommerce since then.