WooCommerce:提交找回密码表单后自定义页面重定向问题
Hey there! Let's fix that password reset redirect without messing with WooCommerce core files (because we all know updates will overwrite those changes faster than you can say "oops").
The Default Behavior
Right now, after a user submits the lost password form successfully, WooCommerce sends them straight to the my-account page via the process_lost_password() method in plugins/woocommerce/includes/class-wc-form-handler.php. But you want them landing on a custom page instead.
The Best Solution: Use a Filter Hook
WooCommerce gives us a handy filter to override this redirect—no core edits needed. Just add this code to your child theme's functions.php file, or a custom plugin (child themes are safer than parent themes, by the way):
/** * Override default lost password redirect URL */ add_filter( 'woocommerce_lost_password_redirect', 'my_custom_lost_password_redirect' ); function my_custom_lost_password_redirect() { // Swap out this URL with your custom page's address return home_url( '/password-reset-success/' ); }
What This Does
- The
woocommerce_lost_password_redirectfilter hooks into the password reset flow right before WooCommerce sends the user off to the default page. - Our custom function tells WooCommerce to use your specified URL instead of
my-account.
The "Not Recommended" Core Edit Route
If you absolutely have to edit the core file (seriously, only do this if you're okay with redoing it every time WooCommerce updates), here's how:
- Open
plugins/woocommerce/includes/class-wc-form-handler.php - Find the
process_lost_password()method - Look for the line that says:
wp_redirect( wc_get_page_permalink( 'myaccount' ) ); - Replace it with your custom URL, like:
wp_redirect( home_url( '/your-custom-thank-you-page/' ) );
But again—don't do this unless you have no other option. Hooks are the way to go for maintainable code.
内容的提问来源于stack exchange,提问作者turboLoop

