Tips, WooCommerce Tutorials

Redirect Users After WooCommerce Login or Registration

It’s possible to redirect your users to a page other than My Account when they login or register. Try adding the following code using the Code Snippets plugin, or via your preferred method.

Redirect at Login

To redirect at login, you need to use the woocommerce_login_redirect filter.

/**
 * Redirect to shop after login.
 *
 * @param $redirect
 * @param $user
 *
 * @return false|string
 */
function iconic_login_redirect( $redirect, $user ) {
	$redirect_page_id = url_to_postid( $redirect );
	$checkout_page_id = wc_get_page_id( 'checkout' );
	
	if( $redirect_page_id == $checkout_page_id ) {
		return $redirect;
	}

	return wc_get_page_permalink( 'shop' );
}

add_filter( 'woocommerce_login_redirect', 'iconic_login_redirect' );

First of all, we’re checking whether the attempted redirect is for the checkout page; this catches users who are logging in at checkout, as it’s likely they will want to go back there and not some other page on the site. If they’re not at checkout, we redirect them to the WooCommerce shop page. You could even use the $user variable to redirect users based on their capabilities.

Redirect after Registration

To redirect after registration, you need to use the woocommerce_registration_redirect filter.

/**
 * Redirect after registration.
 *
 * @param $redirect
 *
 * @return string
 */
function iconic_register_redirect( $redirect ) {
	return wc_get_page_permalink( 'shop' );
}

add_filter( 'woocommerce_registration_redirect', 'iconic_register_redirect' );

 

Avatar

James is the founder of Iconic and an experienced WooCommerce plugin developer. He has been featured on numerous WordPress publications.

He is a keen photographer and currently shoots using his favourite mirrorless camera, a Fuji X-T20.

29 Responses

Avatar

Hi, I'm using woocommerce.com/products/woocommerce-social-login/ and combined with Cloudflare I get this strange behaviour when using social login. The user is being logged in, but the Woocommerce login screen (username + password) is still displayed after the log in. If I navigate to Home and then My-Account it looks like the user had been logged in.

Has anyone experienced this issue?

I followed all the steps available online to exclude the social login from being cached and used redirect plugins and added code in functions/php but to no avail. Any help out there?

Thanks,
Razvan

Avatar
Zaheer Abbas 2 years ago

Below Simple code Worked for me.

function iconic_login_redirect() {
wp_redirect(home_url('/my-page'));
}
add_filter( 'woocommerce_login_redirect', 'iconic_login_redirect' );

Avatar

Hey.

I have a different page for mobile users and desktop users I want to redirect to. Any tips how I can do this? (For checkout its the same for both of them).

Thank you

Avatar

I've tried about 4 snippets and nothing works, including this snippet?

After login I land at the Woo account page, which I don't want.

Why is nothing working? Thanks, in advance.

Avatar

I have the same problem. I used EVERYTHING.

I want to redirect the suscribers in /inicio/ and the redirect is still goingo my-account

Avatar

Hi is this possible to make two redirects after login page
Like i have my clients from east and west and i want them after login if they select east they should redirect to east product and if they select west they should redirect to west products

it would be a great help

Avatar

Make sure you replace:

`add_filter( 'woocommerce_login_redirect', 'iconic_login_redirect' );`

with:

`add_filter( 'woocommerce_login_redirect', 'iconic_login_redirect', 10, 2 );`

otherwise the $user param will not be available in the `iconic_login_redirect` function.

More info here: https://developer.wordpress.org/reference/functions/add_filter/

Avatar

Thanks, that caused a problem for me without the priority (10) and accepted arguments (2)!

Avatar

Hi .
I am using the frontend pm plugin and woocommerce.

If a customer goes to a product on my site there is a "contact seller" button which takes them to the frontend pm page to send a message to the seller. (if they are logged in)

If a customer is not logged in the "contact seller" link will take them to a page saying " you must login to use messages".

Once the customer then logs in they are redirected to there myaccount page.

Is there a way to redirect after login to the frontend pm page they were originally aiming for ?

thanks

Avatar
James Kemp admin 2 years ago

Hey, this article may help: https://www.wpbeginner.com/plugins/how-to-redirect-users-after-successful-login-in-wordpress/

Avatar

Hi,

Thank you very much for the code.
I have a little issue though, I am using the registration redirection code but it's taking me to the home page instead of the specified permalink.

Below are the code I am using:

function iconic_register_redirect( $redirect ) {
return wc_get_page_permalink( 'https://mysite.com/register/' );
}

add_filter( 'woocommerce_registration_redirect', 'iconic_register_redirect' );

I want this to redirect to the register page. I also tried to use 'register' but it's still going to the home pate.

Your assistance is highly appreciated.

Thank you

Avatar
James Kemp admin 2 years ago

Hey,

You're using the `wc_get_page_permalink()` function incorrectly. This function only fetches WooCommerce pages and requires a Woo page slug.

Instead you should try: `return home_url( '/register/' );`

Hope this helps!

Avatar

Login redirection works well. I needed home_url() instead.
Thank you

Avatar

When the Shop is assigned to Front page (home), it's not redirecting to Home/Shop. Presumably because there's no permalink for homepage. What might I do in this case?

Avatar
James Kemp admin 2 years ago

You could just use `get_site_url()` instead of the Woo functions to get the URL?

Avatar

Hi, when I put this my checkout page gives a white page. Also what I really want is that when people come to the checkout page and they are not logged in the are redirected to the my account page to login first and when logged in they automatically come back to the checkout page.

The first part I have, but how to redirect back to the checkout page is missing.
Can you give me a hint?
All the best, Luc

//Woocommerce visible only to logged-in users
function wpse_1_redirect() {
if (
! is_user_logged_in()
&& (is_checkout())
) {
// customize the following line to suit your needs
wp_redirect(site_url('my-account/'));
exit;
}
}
add_action('template_redirect', 'wpse_1_redirect');

Avatar

Got a function which makes all accounts inactive after registration and after that, wp_logout() runs and after that, a redirect to the my account page. Since the user now is registered, logged out and re-directed back to the "My account" page where the login and register form is, do you know how to print a notice message on that page considering the session has ended?

I've tried using this:
if ( ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie(true);

But that does not help. Any ideas or advice?

Avatar

Hi ! Sorry I am a beginner at Woocommerce, website codes, etc. Could you tell me where I should write the code you gave ? Inside what folder, what file ?
Thanks!

Avatar
Edwin Bollen 3 years ago

Either place this in the functions.php file or as James mentions use the Code Snippets plugin. (I prefer to use the last option)

Avatar
James Kemp admin 3 years ago

To confirm, that's the functions.php file inside your theme (preferably child theme). Ideally any custom functionality would go in a plugin, but that's not always straightforward for beginners.

Avatar
Brodie Brodie 3 years ago

A semi colon was missing, this code works.
/**
* Redirect to shop after login.
*
* @param $redirect
* @param $user
*
* @return false|string
*/
function iconic_login_redirect( $redirect, $user ) {
$redirect_page_id = url_to_postid( $redirect );
$checkout_page_id = wc_get_page_id( 'checkout' );

if( $redirect_page_id == $checkout_page_id ) {
return $redirect;
}

return wc_get_page_permalink( 'shop' );
}

add_filter( 'woocommerce_login_redirect', 'iconic_login_redirect' );

Avatar
James Kemp admin 3 years ago

Thanks for the spot! updated the code.

Avatar
Brodie Brodie 3 years ago

That's quick! And thanks for the snippet, it's really useful.

Avatar

Hey, the redirect at login is exactly what I was looking for. Unfortunately this code breaks my site, and I cannot see where the problem lies.

Avatar
James Kemp admin 3 years ago

Sorry about that, semi-colon was missing. Updated!

Leave a Reply

Your email address will not be published. Required fields are marked *