0
  1. I have created login page template and created the login page through wp-admin->pages. here I select the template 'login'

  2. Now I need to redirect user on this page if somebody haven't logged in. wp-admin login and this login page are separate. there is no relation if someone logged in wp-admin or not.

  3. The code which I am using as follows, FireFox redirect me to error page saying "The page isn't redirecting properly".

    if (  !is_admin() ) {
    
    if(!isset($_COOKIE['login_token']) || $_COOKIE['login_token']=='') {
    //echo $_COOKIE['login_token']."KKKKKKKRRRRRRRR";
    $login_page  = home_url('/login/');
    
    // Two things happen here, we make sure we are on the login page
    // and we also make sure that the request isn't coming from a form
    // this ensures that our scripts & users can still log in and out.
    //if( $page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') {
    
     // And away they go...
     wp_redirect($login_page);
    exit();
    
    }   
    $username=$_POST['log']; 
    $password=$_POST['pwd'];
    echo $_COOKIE['login_token'];
    // try to log into the external service or database with username and password
    //$ext_auth = try2AuthenticateExternalService($username,$password);
    //echo "<pre>"; print_r($ext_auth); echo '</pre>'; 
    // if external authentication was successful
    $ext_auth[0] = 'success';
    if($ext_auth[0]=='success') {
    
         // find a way to get the user id
         $uname = explode('@',$username);
         $user_id = username_exists($uname[0]); 
         // userdata will contain all information about the user
         //$userdata = get_userdata($user_id);
         //$user = wp_set_current_user($user_id,$username);
    
         // this will actually make the user authenticated as soon as the cookie is in the browser
        //wp_set_auth_cookie($user_id);
        $path = parse_url(get_option('siteurl'), PHP_URL_PATH);
        $host = parse_url(get_option('siteurl'), PHP_URL_HOST);
        //$expiry = strtotime('+1 month');
        $expiry = time() + (60 * 1);
        setcookie('login_token', $ext_auth[0], $expiry, $path, $host);
         // the wp_login action is used by a lot of plugins, just decide if you need it
        do_action('wp_login',$userdata->ID);
        //determine WordPress user account to impersonate
    
        // you can redirect the authenticated user to the "logged-in-page", define('MY_PROFILE_PAGE',1); f.e. first
        //header("Location:http://executiveboard/audit-blog");
        return 'success';
    
        }
      }
    
    
    }
    

1 Answer 1

2

If a user is not logged in, your code will redirect to the login page even if the user is already in the login page so you end up with a infinite redirection loop.

You should check if the user is logged in and the page where he/she is and redirect if he/she is not logged in and if he/she is not in the login page. Add this code to functions.php:

 <?php
 add_action( 'template_redirect', 'wpse_redirect_to_login_page' );
 function wpse_redirect_to_login_page() {
      //You can use also is_page() function to check for specific page instead for a page template
      if( !is_page_template("login-template.php") && ! is_user_logged_in() ) {
          wp_redirect( get_permalink( ID_of_login_page ); );
          exit();
      }
 }
 ?>

Also, you should take the login/auth process out of the template file. Ore, better just use the wp_login_form in your template file. See How to Make a Custom Login Page.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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