0

I needed to redirect users after login and this was not a problem using an add_filter on functions.php, done, it works as expected. But now I need to avoid this redirect when the user login from a specific page. In short, I need to redirect users after login, but not if they login from a specific page. How can I prevent functions.php to load the login_redirect filter only on that specific page? Here is the simple redirect login in functions.php:

function custom_login_redirect() { 
    return '/main_report'; 
}

add_filter('login_redirect', 'custom_login_redirect');

I searched around the internet but I didn't find a simple solution to this problem. Something I understood is that in functions.php is too early to know which page will be loaded so I guess I must put this hook somewhere else when I can put a condition for the page slug or ID. Thanks in advance for you help.

1 Answer 1

0

You can achieve this by modifying your custom_login_redirect function to include a conditional check based on a parameter you pass from your specific page. Here's an example of how you can modify your function:

function custom_login_redirect($redirect_to, $request, $user) {
    // Check if there is a user and if the user has roles
    if (isset($user->roles) && is_array($user->roles)) {
      // Check if the login request comes from your specific page
      if (isset($_GET['from']) && $_GET['from'] == 'special-page') {
        // Do not redirect
        return $redirect_to;
       } else {
        // Redirect to '/main_report' or another specified page
        return home_url('/main_report');
      }
    } else {
      // Default redirect if there's no user 
      return $redirect_to;
    }
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);

If modifying the login form is not possible, or if you prefer not to use GET parameters for tracking the origin of a login request, you can use PHP session variables as an alternative. This method involves setting a session variable when a user visits your specific page and then checking this variable during the login process.

function set_login_origin_session() {
if (is_page('your-specific-page-slug')) {
    // Start the session if it hasn't already been started
    if (!session_id()) {
        session_start();
    }
    // Set a session variable to indicate the user came from the specific page
    $_SESSION['login_origin'] = 'specific-page';
   }
}
add_action('template_redirect', 'set_login_origin_session');

function custom_login_redirect($redirect_to, $request, $user) {
  // Check if the session variable is set and equals 'specific-page'
  if (isset($_SESSION['login_origin']) && $_SESSION['login_origin'] === 'specific-page') {
    // Clear the session variable to prevent unintended redirects in future logins
    unset($_SESSION['login_origin']);
    
    // Do not redirect; return the original destination
    return $redirect_to;
  } else {
    // Redirect to your default location
    return home_url('/main_report');
  }
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);
3
  • I tried your solution but it doesn't work for me. I just copied yours and replace 'special-page' with my specific page but it keeps redirecting me as in all the other pages. I guess the $_GET['from'] is not finding the right page...
    – zatarra
    Mar 15 at 15:59
  • Thanks Nagi, I had the opportunity to try your code today as I got back from the weekend, but unfortunately I had no luck. The code doesn't work, keeps redirecting me to the default location even when I log in in from the page I don't want to. I did a sort of debug and it seems that the session variable is correctly set, but not retrieved by the custom login redirect function since it keeps ignoring it and redirect to my default location. Also, from my sort of debug, the isset fail the test.
    – zatarra
    Mar 18 at 8:53
  • Nagi, I finally got it to work. Actually the problem was that, for some reason, the URL I need to get is not the current one but the previous through $_SERVER['HTTP_REFERER'] even if I'm using a wp_login_form() and not the wp_login page. So, with a simple custom login redirect with a condition on the $_SERVER['HTTP_REFERER'] I solved my issue and now everything is working as expected. Thanks again for your support!
    – zatarra
    Mar 18 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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