Return Customers to Their Page After Logging In
After logging in on the My Account page, customers go back to the page they were browsing, such as a product or the cart, instead of the account dashboard.
Opens WPIDE on
Code
add_action( 'woocommerce_login_form_end', function () {
$referer = wp_get_referer();
if ( ! $referer || false !== strpos( $referer, 'wp-login.php' ) || untrailingslashit( strtok( $referer, '?' ) ) === untrailingslashit( wc_get_page_permalink( 'myaccount' ) ) ) {
return;
}
if ( wp_validate_redirect( $referer, false ) ) {
printf( '<input type="hidden" name="redirect" value="%s">', esc_url( $referer ) );
}
} );
Notes
Only pages on your own site are used, and customers who came straight to My Account still land on their dashboard.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.