WP_User class用于处理用户信息,是WordPress的核心类。在WordPress插件或主题中获取用户信息时应该使用WP_User class提供的方法,不要直接访问object内容。本文列举了常用的WP_User方法和代码示例。

方法、代码示例和返回结果

测试环境:WordPress 4.9.1

创建WP_User的实例

语法:

$users = new WP_User( $id [, $name [, $blog_id ] ] );

例如:

// 创建一个WP_User的实例,用户ID为22
$user = new WP_User( 22 );

// 创建一个WP_User的实例,用户登录名为TEST-Username
$user = new WP_User( '', 'TEST-Username' );

返回结果:

WP_User Object
(
    [data] => stdClass Object
        (
            [ID] => 22
            [user_login] => TEST-Username
            [user_pass] => $P$BxHrZf4YeOB5wrt2zrUleYaIpoqd7x0
            [user_nicename] => test-username
            [user_email] => test@emaildomain.com
            [user_url] => http://www.youdomain.com
            [user_registered] => 2017-12-06 05:59:56
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => 张 三
        )

    [ID] => 22
    [caps] => Array
        (
            [administrator] => 1
        )

    [cap_key] => wp_capabilities
    [roles] => Array
        (
            [0] => administrator
        )

    [allcaps] => Array
        (
            [switch_themes] => 1
            [edit_themes] => 1
            [activate_plugins] => 1
            [edit_plugins] => 1
            [edit_users] => 1
            [edit_files] => 1
            [manage_options] => 1
            [moderate_comments] => 1
            [manage_categories] => 1
            [manage_links] => 1
            [upload_files] => 1
            [import] => 1
            [unfiltered_html] => 1
            [edit_posts] => 1
            [edit_others_posts] => 1
            [edit_published_posts] => 1
            [publish_posts] => 1
            [edit_pages] => 1
            [read] => 1
            [level_10] => 1
            [level_9] => 1
            [level_8] => 1
            [level_7] => 1
            [level_6] => 1
            [level_5] => 1
            [level_4] => 1
            [level_3] => 1
            [level_2] => 1
            [level_1] => 1
            [level_0] => 1
            [edit_others_pages] => 1
            [edit_published_pages] => 1
            [publish_pages] => 1
            [delete_pages] => 1
            [delete_others_pages] => 1
            [delete_published_pages] => 1
            [delete_posts] => 1
            [delete_others_posts] => 1
            [delete_published_posts] => 1
            [delete_private_posts] => 1
            [edit_private_posts] => 1
            [read_private_posts] => 1
            [delete_private_pages] => 1
            [edit_private_pages] => 1
            [read_private_pages] => 1
            [delete_users] => 1
            [create_users] => 1
            [unfiltered_upload] => 1
            [edit_dashboard] => 1
            [update_plugins] => 1
            [delete_plugins] => 1
            [install_plugins] => 1
            [update_themes] => 1
            [install_themes] => 1
            [update_core] => 1
            [list_users] => 1
            [remove_users] => 1
            [promote_users] => 1
            [edit_theme_options] => 1
            [delete_themes] => 1
            [export] => 1
            [administrator] => 1
        )

    [filter] => 
    [site_id:WP_User:private] => 1
)

 

WP_User::get_data_by( string $field, string|int $value )

获取用户的主要信息,不包含权限、角色等。该方法为静态方法,通过class名称直接调用。

$field可以是:id,ID,slug,email和login。

例如:

$userdata = WP_User::get_data_by( 'email', 'test@emaildomain.com');

返回:

stdClass Object
(
    [ID] => 22
    [user_login] => TEST-Username
    [user_pass] => $P$BxHrZf4YeOB5wrt2zrUleYaIpoqd7x0
    [user_nicename] => test-username
    [user_email] => test@emaildomain.com
    [user_url] => http://www.youdomain.com
    [user_registered] => 2017-12-06 05:59:56
    [user_activation_key] => 
    [user_status] => 0
    [display_name] => 张 三
)

 

WP_User::exists()

根据用户ID是否被赋值判断用户是否存在。

例如:

$user = new WP_User( 22 );
if( $user->exists() ){
	echo "ID为22的用户存在";
} else {
	echo "该用户不存在";
}

 

WP_User::get( string $key )

从users和usermeta表中查询用户数据

例如:

$user = new WP_User( 22 );
$user_email = $user->get( 'user_email' );

有效的参数包括但不限于:

  • ID – 用户id
  • user_login – 用户登录名称
  • user_pass – 用户密码
  • user_nicename – 可在url中安全使用的user_login,例如出现在文章作者链接中
  • user_email – 用户电子邮箱
  • user_url – 用户的站点链接
  • user_registered – 用户注册时间
  • display_name – 公开显示的名字
  • description – 用户介绍

wp_usermeta表中所有的meta_key都可以作为该方法的参数

WP_User::has_prop( string $key )

查询某个用户字段是否存在

例如:

if ( $user->has_prop( 'my_user_key') ){
	echo 'my_user_key 存在';
} else {
	echo '不存在';
}

 

WP_User::to_array()

返回一个数组形式的结果

例如:

$user = new WP_User( 22 );
$user_array = $user->to_array();

打印$user_array得到:

Array
(
    [ID] => 22
    [user_login] => TEST-Username
    [user_pass] => $P$BxHrZf4YeOB5wrt2zrUleYaIpoqd7x0
    [user_nicename] => test-username
    [user_email] => test@emaildomain.com
    [user_url] => http://www.youdomain.com
    [user_registered] => 2017-12-06 05:59:56
    [user_activation_key] => 
    [user_status] => 0
    [display_name] => 张 三
)

 

WP_User::get_role_caps()

获取用户的所有权限

例如:

$user = new WP_User( 22 );
$allcaps = $user->get_role_caps();

得到一个包含用户所有权限的数组

Array
(
    [switch_themes] => 1
    [edit_themes] => 1
    [activate_plugins] => 1
    ...
    ...
    [level_10] => 1
    [level_9] => 1
    ...
    ...
    [delete_themes] => 1
    [export] => 1
    [administrator] => 1
)

 

WP_User::add_role( string $role )
WP_User::set_role( string $role )
WP_User::remove_role( string $role )

给用户增加、设置唯一角色或删除角色,set_role和add_role的区别是,set_role会执行覆盖操作,把其余角色删除,只保留set的那一个角色。

例如:

// 创建一个自定义角色custom_role,注意这句话只需执行一次,比如在插件启用的时候。可以使用插件代替,例如User Role Editor
add_role( 'custom_role', 'Custom Subscriber', array( 'read' => true, 'level_0' => true ) );

// 更改用户角色
$user->add_role( 'custom_role' );
$user->set_role( 'custom_role' );
$user->remove_role( 'custom_role' );

 

WP_User::add_cap( string $cap, bool $grant = true )
WP_User::remove_cap( string $cap )
WP_User::remove_all_caps()

增加一个、删除一个或删除所有用户权限。add_cap()的第二个参数为false,表示给用户增加一个权限,但用户无权使用该权限。

例如:

$user = new WP_User( 22 );
$user->add_cap( 'can_edit_posts' );
$user->remove_cap( 'can_edit_posts' );
$user->remove_all_caps( 'can_edit_posts' );

 

WP_User::has_cap( string $cap )

判断用户是否有某项权限

例如:

$user = new WP_User( 22 );
$user->add_cap( 'can_edit_posts', false );
if ( $user->has_cap( 'can_edit_posts') ){
	echo "用户有can_edit_posts权限";
} else {
	echo "没有can_edit_posts权限";
}

在增加权限时,add_cap()第二个参数设为false,所以程序运行结果是 “没有can_edit_posts权限”。

WP_User::get_site_id()

查询当前用户权限属于哪个站点,返回这个站点的ID。multisite下用户在每个子站可能有不同的权限。


如何获取多个用户

class WP_User用于操作一个用户,要获取多个用户就要借助 class WP_User_Query,或者在它基础上工作的get_users()。

$args = array(
	'blog_id'      => $GLOBALS['blog_id'],
	'role'         => '',
	'role__in'     => array(),
	'role__not_in' => array(),
	'meta_key'     => '',
	'meta_value'   => '',
	'meta_compare' => '',
	'meta_query'   => array(),
	'date_query'   => array(),        
	'include'      => array(),
	'exclude'      => array(),
	'orderby'      => 'login',
	'order'        => 'ASC',
	'offset'       => '',
	'search'       => '',
	'number'       => '',
	'count_total'  => false,
	'fields'       => 'all',
	'who'          => '',
 ); 
get_users( $args );

具体请参考文档:

https://codex.wordpress.org/Function_Reference/get_users

https://codex.wordpress.org/Class_Reference/WP_User_Query

发表评论

电子邮件地址不会被公开。 必填项已用*标注