一,常用函数
获取当前用户ID的方式:
$user_ID = get_current_user_id();
通过邮箱获取用户信息的方式:
$user = get_user_by( 'email', 'user@example.com' );
通过ID获取用户信息的方式:
$user = get_user_by( 'id', '123' );
二,WP_User 类
通过 WP_User 类获取用户信息
$user = new WP_User($id);
方法:
WP_User::get_data_by( string $field, string|int $value )
WP_User::exists()
WP_User::has_prop( string $key )
WP_User::to_array()
WP_User::get_role_caps()
WP_User::add_role( string $role ) WP_User::set_role( string $role ) WP_User::remove_role( string $role )
WP_User::add_cap( string $cap, bool $grant = true ) WP_User::remove_cap( string $cap ) WP_User::remove_all_caps()
WP_User::has_cap( string $cap )
WP_User::get_site_id()
用户信息:
WP_User Object
(
[data] => stdClass Object
(
[ID] => 4
[user_login] => rt_6402b8b489b62
[user_pass] => $P$B71hn.v3FJgST5g3LgFeTIbwNz5dpC.
[user_nicename] => 4
[user_email] => s@foxmail.com
[user_url] =>
[user_registered] => 2023-03-04 03:19:16
[user_activation_key] =>
[user_status] => 0
[display_name] => s******m
)
[ID] => 4
[caps] => Array
(
[subscriber] => 1
)
[cap_key] => wp_capabilities
[roles] => Array
(
[0] => subscriber
)
[allcaps] => Array
(
[read] => 1
[level_0] => 1
[subscriber] => 1
)
[filter] =>
[site_id:WP_User:private] => 1
)