Login Redirects - Help Needed Modifying Code

I have code in functions.php that redirect users to /member-account when logging in:

if( !function_exists('custom_user_login_redirect') ) {
function custom_user_login_redirect() {
$redirect_to = '/member-account';
return $redirect_to;
}
add_filter('login_redirect','custom_user_login_redirect',10,3);
}

This works fine. However, on certain pages, I would like to redirect visitors back to the same page when logging in. I tried doing this with a login shortcode + redirect. It didn't work. Jude mentioned that the code above has a higher priority than the shortcode.

Vaughan was able to accomplish something similar for logins via the Ultimate Facebook plugin (thread). I have tried to use his solution to create a page specific solution for the code above, but I was not successful (I'm not a coder ... so the solution might be simple and I missed it).

Could someone help create a redirect solution for the following scenarios?

1. Global login - All logins redirect to /member-account, except for ...
2. Specific page login - redirects back to the same page

Thanks for your help,
Chris

  • Jude

    Hey Chris

    Hi again ! Looks like you were away for a bit and I needed to close the chat. Can you share all the redirect related snippets here ? I may need to consolidate them and re write it for you to conform to this logic

    Could someone help create a redirect solution for the following scenarios?

    1. Global login - All logins redirect to /member-account, except for ...
    2. Specific page login - redirects back to the same page

    Ideally there should be only one function that handles this logic at the template_redirect hook as I briefly went over during our chat ! Let me know and I can do some tests and post back on this after testing locally

  • Chris

    Jude

    No worries. I had to step away from the desk. I was just in Chat with Kasia. She suggested this thread, which looks promising. But, I'm not sure how to make it work.

    This is what I'm looking to do:

    1. If login from array(example-page-one, example-page-two, example-page-three){
    then, refresh page

    2. } elseif login from 'example-page-four' {
    then, redirect to 'payment-page'

    3. } elseif login from all other pages {
    then redirect to '/member-account'
    }

    I hope this is clear.

    The code I have created so far doesn't work at all.

    Thanks for your help,
    Chris

  • Chris

    I have the following code. The first switch/case (e.g. 'example-page-one') is supposed to log the visitor in and simply refresh the page ... keep visitors on the same page. There is a problem. The visitor is redirected to the homepage. Why is that happening?

    add_action('init', 'after_user_auth');
    
    function after_user_auth() {
    
    switch($_SERVER['HTTP_REFERER']) {
    case 'example-page-one':
    case 'example-page-two':
    case 'example-page-three':
    header("Location: " . $_SERVER['REQUEST_URI']);
    break;
    
    case 'example-page-four':
    return site_url('/example-page-five');
    break;
    
    default:
    return site_url('/member-account');
    break;
    }
    }
  • Chris

    I was able to solve this with the following code:

    if( !function_exists('custom_user_login_redirect') ) {
    function custom_user_login_redirect($redirect_to, $request, $user) {
    $redirect_to = '/member-account';
    
    switch($_SERVER['HTTP_REFERER']) {
    case 'https://example.com/example-page-one/':
    case 'https://example.com/example-page-two/':
    return $_SERVER['HTTP_REFERER'];
    break;
    
    case 'https://example.com/example-page-four':
    return site_url('/example-page-five');
    break;
    
    default:
    return site_url( $redirect_to );
    break;
    }
    }
    add_filter('login_redirect','custom_user_login_redirect',10,3);
    }

    Hopefully that will help someone else trying to accomplish the same thing.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.