I created a members dashboard area on a site recently and needed a custom login page which used the Woocommerce login form to log users into the site. If successful, I wanted them to be redirected to another front-end page. This handy little snippet goes into your functions.php file and allows you to redirect pretty much anywhere:
add_filter('woocommerce_login_redirect', 'pro_login_redirect');
function pro_login_redirect( $redirect_to ) {
$redirect_to = 'http://myexample.com/shop';
return $redirect_to;
}
You can also use existing WordPress calls to send a user to the home URL, front page, blog page. Eg, home page:
add_filter('woocommerce_login_redirect', 'pro_login_redirect');
function pro_login_redirect( $redirect_to ) {
$redirect_to = home_url();
return $redirect_to;
}
Or, how I used it and specified a unique page ID (in this case, page with ID 1020):
add_filter('woocommerce_login_redirect', 'pro_login_redirect');
function pro_login_redirect( $redirect_to ) {
$redirect_to = get_permalink( 1020 );
return $redirect_to;
}
Hey there!
This is a great help.
I have a question though, what would you put after “$redirect_to =” if you want to send the user back to the page they were viewing before clicking login?
I cant find an answer anywhere on the net!
Thanks so much!
Hi Samaz,
In order to do that, you would need to create a new function which referrs the user back to the page they were referred from. Something similar to this:
http://longren.io/wordpress-tip-redirect-to-previous-page-after-login/
Hi Phil,
I have a question, if admin login, how redirect to dashboard? And if other user login, how redirect to account-page or other page?
Personally, I would wrap the function around a basic check for user roles. Something like this:
if ( current_user_can('manage_options') ) {
redirect here
} else {
redirect here instead
}
Phil,
I am building a membership website that is using Groups and Woocommerce Groups to allocate pages based on purchases. What I’d like to do is change the customer login redirect so they go to the one of the pages they have bought. There is an added complication in that they can buy a first level of access that open up a dozen pages to them but then they can buy access to a second level of access, so if they buy level one I want to redirect them to page A from pages a, b and c but when they buy level two of access, and can now see pages a, b and x, y, z, I will need to redirect them to page X.
Do you think that is do-able?
Hi Paul,
While I’ve never actually used the Groups extension for WooCommerce, it should still be possible.
Looking at the plugin page on WooCommerce, each level of membership is allocated an ID, so in theory, you could add a conditional statement to the code above which allows you to check which ID level the user is on and redirect to X page (Page X will have ID X etc)
It all depends on how the extension checks the user level against the ID but I think it could be done. Have you tried contacting the plugin developers? I’m sure it’s something they will be able to help with.
Hi Phil,
Just wondering if you could help.
I have these functions working but wanted to know if it was possible in Woo to redirect a new user to a different page on FIRST login?
So I can have a welcome new user and welcome back page. Other wise I will have to combine the two pages in a more generic welcome.
Possible?
Hi
Thanks for your explanation above. I want to know how to redirect a user to a online appointment scheduling page which should only be visible when the use is logged in.
Appreciate any help.Thanks
Hi Anu,
All the code on this page is aimed at redirecting a user once they have logged in, so the original code above will work, you just need to edit the page they are taken to after logging in:
add_filter('woocommerce_login_redirect', 'pro_login_redirect');
function pro_login_redirect( $redirect_to ) {
$redirect_to = 'http://yourwebsite.com/appointments';
return $redirect_to;
}
I guess you just need to apply some code or membership plugin to make that page only accessible to members, so users who are NOT signed in see some message.