php - Wordpress - Redirect to previous page after login
Get the solution ↓↓↓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 thelogin_redirect
function like so, adapt according to your needs.
add_filter('login_redirect', 'redirect_previous_page', 10, 1);
function redirect_previous_page( $redirect_to ){
global $user;
$request = $_SERVER["HTTP_REFERER"];
if ( in_array( $user->roles[0], array( 'administrator') ) ) {
return admin_url();
} elseif ( in_array( $user->roles[0], array( 'subscriber') ) ) {
return $request;
}
return $redirect_to;
}
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.