Redirect after logging in based on the user role
Not every user should be able to log in to the Dashboard, and WooCommerce already prevents this. But you may want to change it for somebody or just use a different page.
By default WooCommerce redirects the user to the My Account page after a successful login.
You can redirect after logging in and use a custom URL based on the user role, like the Dashboard for admins and My Account page for customers.
Add this code at the end of the file functions.php in wp-content/themes/your-child-theme-name/:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Redirect users to custom URL based on their role after login | |
* | |
* @param string $redirect | |
* @param object $user | |
* @return string | |
*/ | |
function wc_custom_user_redirect( $redirect, $user ) { | |
// Get the first of all the roles assigned to the user | |
$role = $user->roles[0]; | |
$dashboard = admin_url(); | |
$myaccount = get_permalink( wc_get_page_id( 'myaccount' ) ); | |
if( $role == 'administrator' ) { | |
//Redirect administrators to the dashboard | |
$redirect = $dashboard; | |
} elseif ( $role == 'shop-manager' ) { | |
//Redirect shop managers to the dashboard | |
$redirect = $dashboard; | |
} elseif ( $role == 'editor' ) { | |
//Redirect editors to the dashboard | |
$redirect = $dashboard; | |
} elseif ( $role == 'author' ) { | |
//Redirect authors to the dashboard | |
$redirect = $dashboard; | |
} elseif ( $role == 'customer' || $role == 'subscriber' ) { | |
//Redirect customers and subscribers to the "My Account" page | |
$redirect = $myaccount; | |
} else { | |
//Redirect any other role to the previous visited page or, if not available, to the home | |
$redirect = wp_get_referer() ? wp_get_referer() : home_url(); | |
} | |
return $redirect; | |
} | |
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 ); |
Delete the sign <?php
on first line if you are having errors come up after saving the file.
If you need to change the URL for a specific role you can use the function get_permalink() passing the page ID to return its URL:
get_permalink( 150 );
This code will return the URL to the page with ID 150.
Hi Nicola,
Thank you for the script. But I have a question.
How to keep any user on checkout page if he is using the login form located on checkout page?
Thank you,
Hi,
You can check that with
is_checkout()
and eventually change the URL used.Hi Nicola, thank you.
But I tried with is_checkout() and did not work. Then I double checked with Woo support and had this response:
Is it the correct?
Oh yeah, right.
Then you can check if the referer is equal to the checkout page URL and apply the change.
hi! so i have used your sample here as a template to solve my login redirect issues. i made changes as needed and i was hoping you could peak at my work for any errors i may not have noticed? i am just learning wordpress and php so this is a little new for me.
my goal is to have wholesale customers redirect to a separate page. here is what i did:
thank you in advance!
jamie
Hi Jamie,
The code looks good to me!
thank you so much!!! i really appreciate your reviewing this.
You’re welcome!
Jamie, thanks for posting your code. Seems to work well for me, except when login attempt is made from Checkout. The user gets redirected back to shop instead of back into the checkout. Any suggestions?
Hello sir,
how to redirect, payment gateway page cancelled redirect to cart page, cart details shown not empty cart. can you help me please
I’m sorry but I’m not sure to understand what you mean.
Can you please explain?
Hi Nicola,
Thanks for sharing. This is exactly what I was searching for and it worked perfectly.
B.Rgds,
Gabriele
Redirect to previous page after login:
By the way, I wonder why the wp_get_referer() didn’t work.
After searching a lit bit more, I found out that I should use $_SERVER[‘HTTP_REFERER’] in place of wp_get_referer() and it worked fine for me.
So, for anyone else who runs into this problem, use $_SERVER[‘HTTP_REFERER’] in place of wp_get_referer(). I’m not sure why but it works when the other sometimes doesn’t. Hopefully this saves someone else the same hours of trial and errors. 😉
This is the code after my adjustments:
function wc_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( ‘myaccount’ ) );
$home = home_url();
$previoupage = $_SERVER[‘HTTP_REFERER’];
if( $role == ‘administrator’ ) {
//Redirect administrators to the dashboard
$redirect = $dashboard;
} elseif ( $role == ‘shop-manager’ ) {
//Redirect shop managers to the homepage
$redirect = $home;
} elseif ( $role == ‘editor’ ) {
//Redirect editors to the previous page
$redirect = $previoupage;
} elseif ( $role == ‘author’ ) {
//Redirect authors to the previous page
$redirect = $previoupage;
} elseif ( $role == ‘customer’ || $role == ‘subscriber’ ) {
//Redirect customers and subscribers to the previous page
$redirect = $previoupage;
} else {
//Redirect any other role to the previous visited page
$redirect = $previoupage;
}
return $redirect;
}
add_filter( ‘woocommerce_login_redirect’, ‘wc_custom_user_redirect’, 10, 2 );
?>
Hi Nicola! Happy new year!
Sorry to bother you, but… I cannot get this working. 🙁
What if I put the following in my functions.php and why doesn’t it work? WordPress keeps on making me land on /profile.php page… 🙁
function wc_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles;
$myaccount = get_permalink( wc_get_page_id( ‘myaccount’ ) );
//$myaccount = site_url( ‘/mio-account/’, ‘https’ );
if ( ! admin && $role == ‘customer’ ) {
//Redirect customers and subscribers to the “My Account” page
$redirect = $myaccount;
}
return $redirect;
}
add_filter( ‘woocommerce_login_redirect’, ‘wc_custom_user_redirect’, 10, 2 );
Hey Alessandro,
This does not really make sense:
You should get a Fatal error on that because of
! admin
.Hello,
Thanks for your code snippet. It seems to be working well, however, I’d like to somehow redirect users back to the checkout page once logged in/registered successfully. I tried your code and seems to be partially working, and I also tried Gabrielle’s suggested version above, but none of them redirects the user back to the previous/referring page (checkout). They are being redirected to /my-account/.
I’ve been looking for a solution for days, and have tried every single possible snippet I’ve come across, but with no success so far. How can I redirect users to the checkout page (which in this case would be the referrer) after login/registration form submission on My Account page?
So the scenario would be: No logged in user -> checkout page -> login page -> logged in -> checkout page.
Thanks a bunch!
This is very helpful, but I can’t seem to get it to work. I’ve got a role called judge, but logging in as the judge does not redirect.
Here’s the code I’m using:
function wc_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( ‘myaccount’ ) );
if( $role == ‘administrator’ ) {
//Redirect administrators to the dashboard
$redirect = $dashboard;
} elseif ( $role == ‘judge’ ) {
//Redirect judges to panel
$redirect = ‘http://www.somesite.com/wp-admin/admin.php?page=entries-index;’;
} else {
//Redirect any other role to the previous visited page or, if not available, to the home
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( ‘woocommerce_login_redirect’, ‘wc_custom_user_redirect’, 10, 2 );
I just noticed that I had a semi-colon in there at the end of the URL for the judge redirect. But after removing it, it’s still not working.
Hi Andrew,
Right after this line
Add a new line with this code:
Then try to log in with a Judge account and let me know if you see that
here
on your screen somewhere. You can change thehere
to anything you want.Thanks for the help. I ended up finding another way to do it, but I’m going to keep this code handy in case it’s useful elsewhere.
Hi
I’m new to wordpress
Can u plz tell me what changes do I need to make in the script If I want to redirect my site members based on member type I made using “BuddyPress Member Type Generator by Buddydev”?
Thanks so much for this code. Was extremely helpful. We have custom role types for some of our users and I couldn’t get this to work with any redirection plugins, but this has worked a treat.
I am getting a error of “you don’t have access” when i am logging in as a shop-manager.
Thanks very much for the coding – been trying to work out how best to do this for ages for a new site I’m working on.
You’re welcome Pat!
I am actually trying to give subscribers access to the dashboard where they can see the points earned in their course but nothing I have tried seems to work. I copied your code above and changed the line near the end about redirecting customers and subscribers to manage account and replaced it with “to the dashboard.” I was happy to have not actually broken my blog doing this, but nothing changed. Any ideas?
Not a huge deal, but instead of “shop-manger” that role is now called “shop_manager”.
Thank you, works great 🙂
You’re welcome!
thanks for the codes!
I realised the codes work only if logging in from WC my-account page.
what can i do to make the codes work even when logging in from the default login.php?
I used this instead..
/**
* Redirect subscriber after successful login.
*
* @param string $redirect_to URL to redirect to.
* @param string $request URL the user is coming from.
* @param object $user Logged user’s data.
* @return string
*/
function subscriber_login_redirect( $redirect_to, $request, $user ) {
//is there a user to check?
if (isset($user->roles) && is_array($user->roles)) {
//check for subscribers
if (in_array(‘subscriber’, $user->roles)) {
// redirect them to another URL, in this case, the homepage
$redirect_to = home_url();
}
}
return $redirect_to;
}
add_filter( ‘login_redirect’, ‘subscriber_login_redirect’, 10, 3 );
Work like a charm.
Thank you so much for sharing.