特定模板需要登录

时间:2013-11-01 作者:pulla

我有特定的模板来请求登录。

我在函数上尝试了这三个版本。php文件。

#1这不起作用。我不得不这么做add_action( \'wp_head\', \'template_login_permit\' );

因为我把模板目录?如果我输入“wp\\u head”,auth\\u redirect();不工作。

function template_login_permit() {
if ( 
    is_page_template(\'templates/tpl-aa.php\') || 
    is_page_template(\'templates/tpl-bb.php\') ||  
    is_page_template(\'templates/tpl-cc.php\') ||  
    is_page_template(\'templates/tpl-dd.php\') ||  
    is_page_template(\'templates/tpl-ee.php\') ||  
    is_page(\'shop\')
    ) { 

    //echo "ok";
    //auth_redirect();
         if ( !is_user_logged_in() ) {
             //wp_redirect(\'/login\');
             auth_redirect();
             echo "ok";
         }
}

}
   add_action( \'init\', \'template_login_permit\' );

#2

add_action( \'wp\', \'login_redirect\' ); wp适用于所有模板,对吗?那么,如何仅为特定模板进行设置?

    // Redirect users who arent logged in...
function login_redirect() {
  // Current Page
  global $pagenow;
  // Check to see if user in not logged in and not on the login page
  if(!is_user_logged_in() && $pagenow != \'wp-login.php\')
        // If user is, Redirect to Login form.
        //auth_redirect();
      echo "";
 }
 // add the block of code above to the WordPress template
  add_action( \'wp\', \'login_redirect\' );

#这只是为了测试我的自定义登录页面。

add_action(\'init\',\'possibly_redirect\');

function possibly_redirect(){
 global $pagenow;
if( \'wp-login.php\' == $pagenow ) {
 wp_redirect(\'/login\');
 exit();
   }
  }
有没有办法请求特定模板的“登录”(我有自定义登录页面)?

1 个回复
最合适的回答,由SO网友:helgatheviking 整理而成

您的第一个函数刚好处于错误的挂钩上。init 因为查询尚未设置,所以现在就知道您在哪个页面模板/页面上还为时过早。重定向的最佳挂钩通常是template_direct. 以下是我的工作:

function template_login_permit() {

    if (
        is_page_template(\'templates/tpl-aa.php\') ||
        is_page_template(\'templates/tpl-bb.php\') ||
        is_page_template(\'templates/tpl-cc.php\') ||
        is_page_template(\'templates/tpl-dd.php\') ||
        is_page_template(\'templates/tpl-ee.php\') ||
        is_page(\'shop\')
        ) {


            if ( !is_user_logged_in() ) {
                auth_redirect();
            }


    }

}
add_action( \'template_redirect\', \'template_login_permit\' );

结束