Redirection on Custom page

时间:2014-04-20 作者:Xing Lee

我已经创建了登录页面模板,并通过wp admin->页面创建了登录页面。在这里,我选择“登录”模板

如果有人没有登录,现在我需要重定向此页面上的用户。wp admin登录和此登录页是分开的。是否有人登录wp admin没有关系。

我使用的代码如下所示,FireFox将我重定向到错误页面,并说“页面没有正确重定向”。

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 个回复
最合适的回答,由SO网友:cybmeta 整理而成

如果用户未登录,即使用户已经在登录页面中,代码也会重定向到登录页面,因此最终会出现无限重定向循环。

您应该检查用户是否已登录以及他/她所在的页面,如果他/她未登录以及他/她不在登录页面中,则应重定向。将此代码添加到函数。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();
      }
 }
 ?>
此外,您应该将登录/身份验证过程从模板文件中删除。Ore,最好用wp_login_form 在模板文件中。看见How to Make a Custom Login Page.

结束

相关推荐

Redirect not working

我想重定向http://xyz.com/freebooks/ 到http://xyz.com/shop/?min_price=0&max_price=0我将此添加到。htaccess,它会重定向到商店页面,但不会像原始url那样保留/免费图书/或过滤器。.htaccees:RewriteEngine On RewriteRule ^freebooks$ index.php?p=5&min_price=0&max_price=0 [NC,L] # BEGI