Home > Enterprise >  Redirect user after login
Redirect user after login

Time:12-08

I am trying to redirect my users to the last visited page after they logged in on my custom login page, but after users logged in they stay on the login page, and I think it is because of my redirect I have added on my custom login page. Can someone please advise on this? Thank you.

This is my Login redirect on all pages to go to the login page:

function admin_redirect() {

        if ( !is_user_logged_in() ) {
            wp_redirect( home_url('member-login') );
            exit;
         }
    };
    add_action('get_header', 'admin_redirect');

And this is my function I try to redirect to last visited page, if users click on a article for example, to view it if they login. But it is still going to the 'member-login':

function admin_default_page($attributes) {
    $attributes = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/member-login';
    return $attributes;
  }
  
  add_filter('login_redirect', 'admin_default_page');

CodePudding user response:

function admin_redirect() {

    if (is_user_logged_in() ) {
        echo "<script>history.go(-1);</script>";
    } else {
        wp_redirect( home_url('member-login') );
        exit;
    }

}  
  • Related