CMS, 原创, , ,

WordPress 获取当前登录用户信息,密码验证方式,数据库查询

WordPress 获取当前用户信息可以直接使用

$current_user

如果是在函数中调用需要先使用

global $current_user;

获取当前用户登录名:

$current_user->user_login;

获取当前用户密码(加密后的):

$current_user->user_pass;

获取当前用户昵称:

$current_user->user_nicename;

WordPress 查询数据库代码:

global $wpdb; 
$user = $wpdb->get_row( "SELECT * FROM ".wp_users." WHERE user_login ='".$username."'" ); 

WordPress 判断是否为管理员

global $wpdb; 
$user = $wpdb->get_row( "SELECT * FROM ".wp_users." WHERE user_login ='".$username."'" ); 
$capabilities = $wpdb->get_row( "select meta_value from ". $wpdb->usermeta ." where user_id ='". $user->ID ."' and meta_key = '" . wp_capabilities . "'" ); 
$admin = unserialize($capabilities->meta_value); 
if($admin['administrator'] == 1){ 
    //是管理员 
}else{ 
    //不是管理员 
} 

WordPress验证用户密码输入是否正确:

 
//用户输入密码为$password 
$wp_hasher = new PasswordHash(8, TRUE); 
if($wp_hasher->CheckPassword($password, $user->user_pass)){ 
    //密码正确 
} 

(926)

Related Post