Deseja redirecionar os usuários de volta à página que eles estavam visualizando após o login? Embora provavelmente exista um plug-in para isso, criamos um trecho de código rápido que você pode usar para redirecionar o WordPress de volta à página de referência após o login.
Instruções:
Tudo o que você precisa fazer é adicionar esse código ao arquivo functions.php do seu tema ou em um plug-in específico do site:
if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) { add_filter('login_redirect', 'my_login_redirect', 10, 3); function my_login_redirect() { $location = $_SERVER['HTTP_REFERER']; wp_safe_redirect($location); exit(); } }
Observação: Se esta é a primeira vez que você adiciona trechos de código no WordPress, consulte nosso guia sobre como copiar/colar corretamente trechos de código no WordPress para não danificar seu site acidentalmente.
Se você gostou desse snippet de código, considere dar uma olhada em nossos outros artigos no site, como: 62 melhores temas de blog gratuitos do WordPress e como criar um formulário de contato no WordPress.
Thank you for your article.
Could you give the complete code snippet for the same action with WooCommerce because I tried adding the different pieces of code but I’m afraid I’ve done some errors. The result has failed.
Thank you very much in advance.
And is there the same code for the logout process?
I want any user that login from a page say https://example.com/asset/* is redirected back to that page after login. What do i have to do to make this work
Are you only wanting to only redirect on https://example.com/asset/* ?
The above snippet should redirect the page URLs in general.
Hello, does the code work well with Ultimate Member plugin?
Hello,
I can’t make it work.
I use this:
add_action(‘init’,’custom_login’);
function custom_login(){
global $pagenow;
$substitute_login_page = str_replace(‘wp-login.php’,’my-account’, $pagenow);
if( ‘wp-login.php’ == $pagenow && $_GET[‘action’]!=”logout” && $_GET[‘action’]!=”lostpassword”) {
wp_redirect($substitute_login_page);
exit();
}
}
to use the Woocommerce login page instead wp-login.php (my website is online shop). And it works.
I try to return user in blog post/single product page viewed before login using the filter listed here ( add_filter(‘woocommerce_login_redirect’, ‘wc_login_redirect’); ), but it’s not working.
I guess the filter and my action can’t work together. I think Woocommerce login page strips redirect part from url.
How can I achieve redirect to previous page after login in my-account page?
Thank you in advance!
Sadly there could be a number of causes. You may want to try a different priority for the filter.
WOW!! that one worked for me perfectly. thankyou
Glad it helped, Vinoth
How do I have to modify this code to redirect users after a sign up?
Hey Ole, I hope this snippet can help you out: https://www.isitwp.com/redirect-a-successful-registration-page/
If you have further questions, please feel free to reach out.
Hello sir,
I am using my own code for registration and login. How to use there the above mention code.Plz help
i want to this other way
i want to show the product prior to download or purchase want to make login in easy digital download
Not sure, I totally understand what you are looking for. Can you go into a little more detail.
I want to show login to download button first and if user register and login then site should show prices and if clicked it should go to download as showed in image
https://snag.gy/rTRloi.jpg
This does not work for me using WP 4.1.1 with the FULLBY theme and Simple WordPress Membership plugin
Hi, this didn’t change anything for me. Any suggestions? I’ve been looking for this solution for a while.
when you login to test this script what page are you on? are you on the wp-login.php page, or another page.
Yes, on the wp-login.php page.
This would be the reason, the idea behind this snippet is that if you have a login form in your sidebar or another place for example. I would be reading the post then enter my details to login, I would then login and be forwarded back to the page I login from.
Okay, thank you. So, I am just using the standard WP Meta Login widget. When in a post before commenting you must login. In the sidebar is the Meta/Login widget which takes you to the wp-login.php page when you click ‘Log In.’ Once logged in on the wp-login.php page, it returns the visitor to the Homepage always, as opposed to the post they were viewing beforehand. Please let me know if your code will work somehow for this. Thank you!
Hi Karena,
send me a message via the contact form, will be easier. Comments can get a bit long.
I too really need this. My news page restricts contents to subscribers only. When a subscriber loads up an article, the error message is displayed but they can always login using the widget form in the sidebar. My problem is that when they successfully login they are not redirected to the last article they are in.
Does anyone know how to do this for a woo-commerce login?
Give this a try,
add_filter(‘woocommerce_login_redirect’, ‘wc_login_redirect’);
function wc_login_redirect( $redirect_to ) {
$redirect_to = ‘http://mysite.com/shop’;
return $redirect_to;
}
But to redirect you back to the referring page, not a static page?
should be able to use the code in the above sample, but I would first check to make sure that login code works then just replace
$redirect_to = ‘http://wpsnipp.com’;
return $redirect_to;
with
$location = $_SERVER[‘HTTP_REFERER’];
wp_safe_redirect($location);
exit();
What is the use of $redirect_to after replacement. Code is not working for me 🙁
Hi Aayush, this should work without any issues, when a user logs into woo commerce they will be redirected in this case to wpsnipp.com. Place this within the functions.php of your wordpress theme, also make sure that you have woo commerce installed otherwise this will not work.
add_filter(‘woocommerce_login_redirect’, ‘wps_login_redirect’);
function wps_login_redirect( $redirect_to ) {
$redirect_to = ‘http://wpsnipp.com’;
return $redirect_to;
}
Hello Kevin,
I tried this code and it works:
add_filter(‘woocommerce_login_redirect’, ‘wps_login_redirect’);
function wps_login_redirect( $redirect_to ) {
$redirect_to = ‘http://wpsnipp.com’;
return $redirect_to;
}
But this one didn’t work:
add_filter(‘woocommerce_login_redirect’, ‘wps_login_redirect’);
function wps_login_redirect( $redirect_to ) {
$location = $_SERVER[‘HTTP_REFERER’];
wp_safe_redirect($location);
exit();
}
I would appreciate if you could help me,
Regards
I would guess that HTTP_REFERER is empty.
http://php.net/manual/en/reserved.variables.server.php
The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature.
Thank You. It is worked for me
Has anyone had success w/ this code integrated w/ any S2Member websites? The way the S2Member plugin handles the post-login redirect may be overriding the code suggested.
For good measure, in addition to creating a functionality plugin, I’ve updated the functions.php file itself within my parent theme, as well as the functions.php within the child theme which my site actually uses. I made sure that the post variable ID was being updated for the one that the WordPress login page actually uses (“user_login”), as opposed to “login_location”.
Looking forward to hearing of any experience w/ successfully redirecting w/ sites that have the S2Member plugin installed and activated.
Cheers,
Darren
Hi Darren,
s2 has some built in functions for login redirect eg:
add_filter(“ws_plugin__s2member_login_redirect”, “s2_redirect”, 10, 2);
function s2_redirect($redirect, $vars = array()){
// bla bla bla code……
return $redirect;
}
I have not tested this but something along this lines could work for you.
if ( (isset($_GET[‘action’]) && $_GET[‘action’] != ‘logout’) || (isset($_POST[‘login_location’]) && !empty($_POST[‘login_location’])) ) {
add_filter(“ws_plugin__s2member_login_redirect”, “s2_redirect”, 10, 2);
function s2_redirect($redirect, $vars = array()){
$redirect = $_SERVER[‘HTTP_REFERER’];
return $redirect;
}
}
This doesn’t work for me. Incompatible with 3.8.1?
This doesn’t work for me. Incompatible with 3.8.1?
This doesn’t work for me. Incompatible with 3.8.1?
Wouldn’t it logically be more correct to simply use
add_filter(‘login_redirect’, ‘my_login_redirect’, 10, 3);
And then move the if statement into the function itself?
Which page would I put this on? Are we talking about a page w/ a link to the login page, the login page, and the redirected page?
Place this into the functions.php and will redirect them to the page they had been viewing after they login.
AWESOME! Even better than I was thinking it could be…
Great! Thanks
Cool glad that you like the snippet.
Nice!
Ya I agree Justin! – Tyler wrote this after needing it for work I’m sure ill be using this one for a project soon.