Thank you for reading! There’s a lot redirecting going on on the site I’m creating.
For example,
-
if not logged in user access a “private” page, he will be redirected to a custom login page.
-
if the user logs in, he will be redirected to the last page he visited before accessing the login form.
Let’s call the pages “Profile” and “LogIn”.
So, if everything goes well should be:
-> not logged try to access Profile -> redirect to LogIn -> if OK redirect to Profile
However, when LogIn calls wp_get_referer(), it does NOT return Profile. Instead it goes “2 pages” back.
Is like “wp_get_referer()” is not seing Profile as the referer because is acting as a redirection. Just like the “Back” button in the browser would.
If you go to the LogIn through any ‘public’ page (clicking the link) the redirect after login in works fine.
page-profile.php
if( !is_user_logged_in() ) {
wp_redirect( get_permalink(37) ); //id 37 = page-login.php
exit;
}
page-login.php
//Set referer
$referer = wp_get_referer();
if( !$referer || referer_is_login_page($referer) )
$referer = get_bloginfo('url');
simpleSessionSet('login_referer', $referer);
...
//Log the user in
...
if ( is_wp_error($user_id) )
$errores = TRUE;
else
wp_redirect( simpleSessionGet('login_referer', '') );
Don’t bother with the simpleSessionGet, instead do what WordPress itself does and use a get parameter
e.g.
Then in your redirect, do something similar to:
Thanks to @Tom J Nowell I came up with this, which keeps the essence of the code and leaves the URL much cleaner, beacuse in the majority of cases $_GET is not needed
page-profile.php
page-login.php