当我运行wp_signon()时,我有几个类似的警告: "警告:无法修改标头信息 - 已经发送的标头(输出从/web/htdocs/www.example.com/home/wp-includes/class.wp-styles.php:237开始)第892行" / / em> /web/htdocs/www.mysite.com/home/wp-includes/pluggable.php 无论如何都要登录,如果我改变页面然后没有给我带来任何问题,但我无法理解这些错误。 我正在附加登录代码。 非常感谢您的帮助!
header.php:
<div id="loginbar">
<?php $current_user = wp_get_current_user();
if(isset($_POST["submit"])){
custom_login();
}?>
<?php if(!is_user_logged_in()): ?>
<form name="frontlog" method="post">
<input id= "log" type="text" name="log" placeholder="Username">
<input id= "pwd" type="password" name="pwd" placeholder="Password">
<input type= "submit" name="submit" class="linkbuttonleft" id="loginbutton">
</form>
<a class="linkbuttonright" title="Registrati" href="http://www.example.com/registrazione/">
<img id="chiave" src="http://www.example.com/wp-content/themes/BiscuitsTheme/immagini/chiave.png" onmousedown="return false"></a>
<?php else: ?>
<p> <?php echo 'Benvenuto ' . $current_user->user_login;?> </p>
<a class="linkbuttonleft" title="Logout" href="<?php echo wp_logout_url( home_url() ); ?>"></a>
<img id="lucchetto" src="http://www.example.com/wp-content/themes/BiscuitsTheme/immagini/lucchetto.png" onmousedown="return false"></a>
<a class="linkbuttonright" title="Profilo" href="http://www.example.com/profilo/">
<img id="profilo" src="http://www.example.com/wp-content/themes/BiscuitsTheme/immagini/profilo.png" onmousedown="return false"></a>
<?php endif; ?>
</div>
functions.php:
function custom_login() {
$creds = array();
$creds = array(
'user_login' => $_POST["log"],
'user_password' => $_POST["pwd"],
'remember' => true
);
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ) {
echo $user->get_error_message();
}
else {
wp_set_current_user( $user->ID, $user->name );
wp_set_auth_cookie( $user->ID, true, false );
}
};add_action( 'after_setup_theme', 'custom_login' );
答案 0 :(得分:0)
最明显的问题可能是
<div id="loginbar">
<?php $current_user = wp_get_current_user();
if(isset($_POST["submit"])){
custom_login();
}?>
登录功能将加载一个新的页面,一旦有数据输出到页面,php就无法做到这一点,你在函数上面有1个div,我也假设你的标题输出。
尝试使用函数文件(主题或插件)并使用init hook:
function cust_log(){
$current_user = wp_get_current_user();
if(isset($_POST["submit"])){
custom_login();
}
add_action('init', 'cust_log');
答案 1 :(得分:0)
我已经解决了“第一次没有登录错误”的wp_signon()函数,所以你只需要在wp_signon()函数下面调用set_current_user($ user_verify-&gt; ID)函数:
require('wp-load.php');
$err = '';
$success = '';
global $wpdb,$current_user;
$response = array();
$data = json_decode(file_get_contents("php://input"));
$email = $data->email_id;
$password = $data->password;
$username = $wpdb->escape($email);
$password = $wpdb->escape($password);
$remember = true;
if($remember) $remember = "true";
else $remember = "false";
$login_data = array();
$login_data['user_login'] = $username;
$login_data['user_password'] = $password;
$login_data['remember'] = $remember;
$user_verify = wp_signon( $login_data, false );
set_current_user($user_verify->ID);
if (is_user_logged_in())
{
get_currentuserinfo();
$response['user_id']=$user_verify->ID;
$response['first_name']=$current_user->user_firstname ;
$response['last_name']=$current_user->user_lastname;
$response['email']=$current_user->user_email;
$status="success";
$msg="";
} else {
$status="failed";
$msg="Invalid Credential.";
}