1

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', '') );
  • 1
    Is this login page the default login page or one of your own creation? Also where is the code? – Tom J Nowell Jul 4 '13 at 12:25
  • It's my own creation. If you go to the login through any other page (clicking the link) the redirect works fine. If you go to the login via redirection then comes the problem – bysanchy Jul 4 '13 at 13:18
1

Don't bother with the simpleSessionGet, instead do what WordPress itself does and use a get parameter

e.g.

$current_url = $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
wp_redirect( get_permalink(37).'?redirect_to='.$current_url);

Then in your redirect, do something similar to:

wp_redirect( $_GET['redirect_to'] );
  • Works but it's not what I need. Thank you a lot for your time – bysanchy Jul 4 '13 at 13:56
  • How is it not what you need? It solves your problem.... – Tom J Nowell Jul 4 '13 at 14:02
0

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

if( !is_user_logged_in() ) {    
    $redirect = url_add_parameter( get_permalink(37), 'redirect_to='.get_permalink() ); // Adds a parameter to the URL checks if '&' or '?' are being used already
    wp_redirect( $redirect );
    exit;
}

page-login.php

// Set referer
if( $_GET['redirect_to'] ) 
    $referer = $_GET['redirect_to']; //When the referer page was a redirection
else
    $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) )            
    $error = TRUE;
else 
    wp_redirect( simpleSessionGet('login_referer', '') );
  • URL even cleaner: page-profile.php $redirect = url_add_parameter( get_permalink(37), 'to='.get_the_ID() ); - **page-login.php** if( $_GET['to'] ) $referer = get_permalink($_GET['to']); – bysanchy Jul 4 '13 at 14:50
  • This fails on search, category, author, tag, and other archive listings – Tom J Nowell Jul 4 '13 at 15:56
  • Does not. "search, category, author, tag, and other archive listings" do not need to set a redirection to the login because they're public pages. In public pages you will go to the login by clicking the login link. Therefor wp_get_referer() will work perfectly – bysanchy Jul 4 '13 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.