skip to Main Content

In my WordPress v5.8.2, I have a custom login page http://www.....com/login.

Site allows only logged in users to post comments and below redirect URL link is published on the comments section for user to login:

http://www......com/login/?redirect_to=http://www......com/post-name

I am trying to redirect back to the post after login with below function:

function redirect_to_posts_after_login($redirect_to, $requested_redirect_to) {
    $redirect_to_post = $_GET['redirect_to'];
    if (!$redirect_to_post === "") {
        return $redirect_to_post;
    } else {
        return $requested_redirect_to;
    }
}

add_filter('login_redirect', 'redirect_to_posts_after_login', 10, 3);

However, it still redirects to wp-admin after the login. How can I make the redirect back to the post?

3

Answers


  1. Chosen as BEST ANSWER

    With below changes, now I am able to return the user to the post after login.

    Login URL at each post remained same as below with redirect_to:

    http://www......com/login/?redirect_to=http://www......com/post-name
    

    In my custom login page template login I am capturing the redirect_to value as a variable:

    $redirect_back_to = $_GET['redirect_to'];
    

    And then in the same custom login page I have this hidden input field that take the redirect_to as input value:

      <input type="hidden" value="<?php echo $redirect_back_to; ?>" name="redirect_to_post_url">
    

    In the below function I am capturing the post referrer URL from the above input and checks if there is a referrer (redirect_to) it will redirect to the post else to wp-admin:

    function redirect_to_posts_after_login($redirect_to) {
        $redirect_to_post = filter_input(INPUT_POST, 'redirect_to_post_url');
    
        if (!$redirect_to_post === "") {
            wp_redirect(esc_url($redirect_to_post));
        } else {
            return $redirect_to;
        }
    }
    
    add_filter('login_redirect', 'redirect_to_posts_after_login', 10, 1);
    

  2. Since you are trying to redirect the user, to your desired custom URL after login. Then you should insert your custom URL here:-

        if ( isset( $user->roles ) && is_array( $user->roles ) ) {
                //check for admins
                if ( in_array( 'administrator', $user->roles ) ) {
                    // redirect them to the default place
                    return $redirect_to;
                } else {
                    //you shud concatinate your custom url here
                    return home_url();
                }
            } else {
                return $redirect_to;
            }
    

    $redirect_to variable contains the URL to the wp-admin. But as a user apart from admin. You can insert the custom URL beside the home_url() function.(Concatinate your custom url in the form hard coded string)

    Login or Signup to reply.
  3. !$redirect_to_post === "" will always return false !

    can you try with this code?

    function redirect_to_posts_after_login($redirect_to, $requested_redirect_to) {
        $redirect_to_post = $_GET['redirect_to'];
        if ($redirect_to_post !== "") {
            return $redirect_to_post;
        } else {
            return $requested_redirect_to;
        }
    }
    
    add_filter('login_redirect', 'redirect_to_posts_after_login', 10, 3);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search