1

I have created a custom login page, different from the default login page. The custom login page is working flawlessly without any errors and redirects after login to a specified page.

However, I need to prevent access to this login page when users are logged in to wordpress and for any reason attempt to view the login page again. The user should be redirected to the home page or any other page I specify. I have used the get_queried_object() function to return the page object's post title element ('Login' in this use case).

The conditional check that should force a redirect when the $page_title = 'Login' as shown in code below does nothing as I can still access the login page even when I am signed into the site.

function redirect_login_page() {
   $page_object = get_queried_object();
   $page_title = get_queried_object()->post_title;
   $login_page  = home_url( '/login/' );
   $home_page  = home_url();
   $nf_page  = home_url('404');
   $page_viewed = basename($_SERVER['REQUEST_URI']);

    if (!is_user_logged_in() ) {
        if( $page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
            wp_redirect($login_page);
            exit;
       }
    }
    else
    {
      if( ($page_viewed == "wp-login.php" or $page_viewed == "login.php" OR $page_title == 'Login') && $_SERVER['REQUEST_METHOD'] == 'GET') {
      wp_redirect($home_page);
      exit;
     }
    }
}

add_action('init','redirect_login_page');

How may this redirect be enforced for already logged in users.

2

You should redirect as early as possible. The best hook for this is template_redirect. Check for 2 conditions - if the page is 'login' and user is logged in:

function redirect_login_page() {

    if( is_page( 'login' ) && is_user_logged_in() ) {
        wp_redirect( home_url() );
        exit;
    }

}
add_action( 'template_redirect', 'redirect_login_page' );
  • Pure code dumps are not recommended, it's preferred to explain things as well. Thanks – birgire Nov 8 '16 at 10:09
  • 1
    I understand this, but the code is pretty self-explanatory and looks like the author is developer, technically skilled enough to read and understand it. However, I've added a simple explanation. – Ihor Vorotnov Nov 8 '16 at 10:15
  • yes better, thanks - Other visitors to this site will read your answer as well ;-) PS: I noticed your answer because someone had flagged it as low quality, possibly because of the missing explanation. – birgire Nov 8 '16 at 10:20
  • @IhorVorotnov, it appears I was attempting to plugin to the init action hook, which is triggered after WordPress has finished loading and therefore, it is too late to do a redirect right? Hooking into the template_redirect action hook also works, when the get_queried_object() function is used to the get the page's post_title, and checking for `if($page_title == 'Login') as per my use case, highlited above. Thanks for the solution. – Terungwa Nov 8 '16 at 10:34
  • @Terungwa, yes, init is too late for redirects. Also, I don't really see a reason to check for title (as it may change over time), when you have url slug (there's a bigger chance it will stay the same for SEO purposes). Anyway, it's for you to decide. – Ihor Vorotnov Nov 9 '16 at 9:00

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.