自定义登录页--wp_SignOn标头已发送?

时间:2011-08-25 作者:Chiubaka

正如标题所示,我正在为我的网站用户构建一个定制的前端登录页面。我正在使用wp\\u signon在验证用户的凭据后登录,但出现以下错误:

Warning: Cannot modify header information - headers already sent by (output started at C:\\xampp\\htdocs\\wordpress\\wp-content\\themes\\twentyeleven\\header.php:13) in C:\\xampp\\htdocs\\wordpress\\wp-includes\\pluggable.php on line 737
twentyeleven\\标题。php:13显示了这一点(包括上面和下面的行):

<!--[if IE 6]>
<html id="ie6" <?php language_attribute(); ?>>
<![endif]-->
我读过一两篇文章说,wp\\u登录必须在创建任何页面内容之前发生,因为它会改变标题,但我想我不太确定当时该如何使用它。wp\\U signon应该去哪里?以下是我的代码结构:

//Retrieve submitted $_POST values.
//Check if this is the first time the form has been displayed (there is a hidden input at the end of the form that changes a boolean to false for all future submits)
//If it\'s not the first time, do form validation and create an array with error ID\'s to be displayed in the form. One of these possible errors is wrong username/password combo:
if (!get_userdatabylogin( $submitted[\'email\'] ))
     $errors[\'emailIsValid\'] = false;
else
{
     $user_data = get_userdatabylogin( $submitted[\'email\'] );
     $password_hashed = $user_data->user_pass;
     if (wp_check_password($submitted[\'password\'], $password_hashed, $user_data->ID))
     {
          $credentials = array();
          $credentials[\'user_login\'] = $submitted[\'email\'];
          $credentials[\'user_password\'] = $submitted[\'password\'];
          if (empty($submitted[\'rememberme\']))
               $credentials[\'remember\'] = false;
          else
               $credentials[\'remember\'] = true;
          wp_signon($credentials, true);
     }
     else
          errors[\'passwordMatchesEmail\'] = false;
}
//Check to see if the errors array has any flags, if so flag a variable to say errors exist
//Form code: form submits to itself
//Check error flags and display error message accordingly
//Display body of form
除此之外,一切都很好wp_signon 线输入正确的凭据会产生我上面提到的错误。

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

您必须在请求中提前挂接以登录某人,您无法从页面模板中执行此操作,因为正如错误所述,标题已经发送到浏览器,因此此时为时已晚。

<?php   
add_action(\'template_redirect\', \'my_check_login\');

function my_check_login(){
    if( is_page(\'my-login-page\') && isset($_POST[\'email\']) ){
        // check input and
        // do my login stuff here   
    }
}

结束