Redirect WooCommerce log-in form
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.godaddy.com/resources/skills/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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ); |