函数文档

set_current_user()

💡 云策文档标注

概述

set_current_user() 是一个已弃用的 WordPress 函数,用于通过用户 ID 或用户名更改当前用户。自 3.0.0 版本起,建议使用 wp_set_current_user() 替代。

关键要点

  • 函数已弃用:自 WordPress 3.0.0 起,set_current_user() 被标记为弃用,应改用 wp_set_current_user()。
  • 功能描述:通过用户 ID 或用户名设置当前用户,如果不知道用户 ID,可将 $id 设为 null 并指定用户名。
  • 参数说明:$id(整数或 null,必需)为用户 ID,$name(字符串,可选)为用户名。
  • 返回值:返回 WP_User 对象,实际调用 wp_set_current_user() 实现。
  • 相关函数:wp_set_current_user() 是替代函数,_deprecated_function() 用于标记弃用。

代码示例

function set_current_user($id, $name = '') {
    _deprecated_function( __FUNCTION__, '3.0.0', 'wp_set_current_user()' );
    return wp_set_current_user($id, $name);
}

注意事项

  • 弃用警告:使用此函数会触发弃用通知,建议更新代码以避免兼容性问题。
  • 版本历史:该函数在 2.0.1 版本引入,3.0.0 版本弃用。

📄 原文内容

Changes the current user by ID or name.

Description

Set $id to null and specify a name if you do not know a user’s ID.

See also

Parameters

$idint|nullrequired
User ID.
$namestringoptional
The user’s username

Return

WP_User returns wp_set_current_user()

Source

function set_current_user($id, $name = '') {
	_deprecated_function( __FUNCTION__, '3.0.0', 'wp_set_current_user()' );
	return wp_set_current_user($id, $name);
}

Changelog

Version Description
3.0.0 Deprecated. Use wp_set_current_user()
2.0.1 Introduced.