您的当前位置:首页 > 知识博客 > wordpress建站

WordPress更改当前用户函数:set_current_user

时间:2024-03-11

WordPress函数set_current_user用于更改当前登录用户,在一些需要代登录的场景比较有用。自WordPress 3.0.0版本起,已不建议使用set_current_user()函数,应该使用wp_set_current_user()函数。

set_current_user( int|null $id, string $name = '' )

函数参数

$id

整数

用户ID

$name

字符串

用户登录名

函数使用示例

$user_id = 2;
$user = get_user_by('id', $user_id);
if($user) {
    wp_set_current_user($user_id, $user->user_login);
    wp_set_auth_cookie($user_id);
    do_action('wp_login', $user->user_login, $user);
}

扩展阅读

set_current_user()函数位于:wp-includes/pluggable-deprecated.php

相关函数:

  • wp_set_current_user()


欢迎咨询/Welcome to inquire
tel/vx:18842938855
qq:1685522781
email:1685522781@qq.com

Copyright © 2019-2024 sex.cyou


Whatsapp
Powered by RRZCMS