Skip to main content
4 of 7
added 15 characters in body
bysanchy
  • 110
  • 1
  • 12

wp_get_referer not working properly after wp_redirect

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 other 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 ) {
    simpleSessionSet('login_referer', get_bloginfo('url'));
} elseif( !referer_is_login($referer) ) {   
    simpleSessionSet('login_referer', $referer);
}

...

if ( is_wp_error($user_id) )            
    $errores = TRUE;
else {
    wp_redirect( simpleSessionGet('login_referer', '') );

}
bysanchy
  • 110
  • 1
  • 12