Redirect WooCommerce log-in form

ksenia kudelkina 7104 unsplash

I started researching how to redirect the user after they log in through the WooCommerce log-in form after I’d embedded the woocommerce_my_account shortcode in a project and users were being directed off to the wp-admin/admin-ajax.php url. Clearly this wasn’t good.

Anyhow, you can filter the page to redirect users off like this:

function cwc_login_redirect( $redirect, $user ) {
return home_url();
}
add_filter( 'woocommerce_login_redirect', 'cwc_login_redirect', 10, 2 );

So that redirects users to the home page after logging in. You could send them to any page you liked – here is how to get some useful WooCommerce page urls, including the Shop, Cart and Checkout page urls: https://www.skyverge.com/blog/get-woocommerce-page-urls/.

If you want to know how to ensure you don’t hit the admin-ajax.php page, you can do this:

function cwc_login_redirect( $redirect, $user ) {
if( strpos( $redirect, 'wp-admin/admin-ajax.php' ) !== false ) {
$redirect = home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'cwc_login_redirect', 10, 2 );

Leave a Reply

Your email address will not be published. All fields are required.