0

I have created a separate login page on /login/ and have wp-login.php redirecting there as well - I have also hooked into a few WP login related hooks such as wp_login_failed so that people get redirected there.

However I have now decided I want admins to continue to be able to login from the standard place and keep this for users - no problem here as I can just direct users to /login/ and remove the redirects.

Problem with doing that is on error users are going to get redirected to /wp-login.php again and if I put the redirects back then admin users will get directed to /login/ instead.

How to handle this?

  • How do you think you can know whether someone is an admin before he logs in? – flomei Feb 22 '16 at 12:54
  • @flomei Well that's why you have a separate login for admins only. I guess a normal user could try and login from that page if they wanted - point is the user login page is for a separate member area and isn't really related to logging into the WP panel (it's about user experience for the admin). – Brett Feb 22 '16 at 13:14
  • Do you use the same login process (= the one build into WP) for users and admins? – flomei Feb 22 '16 at 14:16
  • @flomei I have made use of wp_login_form to create the additional login page. – Brett Feb 22 '16 at 14:38
  • Are the users saved in the same database, meaning _users table? – flomei Feb 22 '16 at 14:43
1

You can do this using login_redirect filter.

function my_login_redirect( $redirect_to, $request, $user ) {
    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            //make wp-admin as your default place, if you customized it
            return $redirect_to;
        } else {
            return home_url('/login/');
        }
    } else {
        return $redirect_to;
    }
}

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

UPDATE

I am assuming that you are using wp_login_form for the custom login page and already hooked to authenticate to handle empty username and/or password issue.

We can use $_SERVER['HTTP_REFERER'] to check from where the request came from and redirect accodringly.

function wpse9845_my_awesome_login_fail( $username ) {
   $referrer = $_SERVER['HTTP_REFERER'];  // where did the post submission come from?
   // if there's a valid referrer, and it's not the default wp-login.php ot wp-admin screen
   if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
      wp_redirect( $referrer );
      exit;
   }
}
add_action( 'wp_login_failed', 'wpse9845_my_awesome_login_fail' );

There's a bad side of the above code: $_SERVER['HTTP_REFERER'] is not supported by all user agent.

Note(from php.net): HTTP_REFERER - The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

Best way to do this is using a completely custom login page/form, along with authentication. Here is a very good tutorial.

  • 2
    Doesn't that just redirect them after a successful login and hence you already know if they're and admin or not; doesn't give them a separate place to login. – Brett Feb 22 '16 at 13:15
  • @Brett okay, looks like i misunderstood you. I thought, you wanted to redirect admin users to wp dashboard, and others to some other place. Why would you want separate login forms, while you can easily do different action for different type of action, using only one login form? – тнє Sufi Feb 22 '16 at 18:13
  • Because it's about providing a good user experience. One section may be called "Member Login" styled like the site and the other could just be the normal wp login page. – Brett Feb 23 '16 at 13:38
  • @Brett How are you handling your custom login page? Did you use wp_login_form? Update your question with custom login form codes, authentication process and hooks you have done. – тнє Sufi Feb 24 '16 at 11:17
  • @Brett check my update. – тнє Sufi Feb 24 '16 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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