php - Wordpress - Redirect to previous page after login
On Woocommerce for Wordrpress, when you're on a product page and click to login, when you're login, you're redirected to your Dashboard page from your account and not the previous page that you was reading.
I'm trying to do that, redirect to the previous page before login or to home page if there is no previous page on my site.
I'v tried this but every time I'm redirected to the home page.
function wc_custom_user_redirect( $redirect ) {
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
Any idea ?
Answer
Solution:
You can filter the
login_redirect
function like so, adapt according to your needs.Answer
Solution:
I had a similar issue. It comes in handy when you're linking to a product page from an e-newsletter and don't allow guest checkout. I bypassed it using a plugin that brought the login up as a pop-up window - if it doesn't leave the page it doesn't need to redirect.