# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, login, redirects, my-account
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-login-redirect-back/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Return Customers to Their Page After Logging In" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
