wp_login()
云策文档标注
概述
wp_login() 函数用于验证用户登录信息并在验证成功后登录用户。该函数已弃用,建议使用 wp_signon() 替代。
关键要点
- 函数已弃用:自 WordPress 2.5.0 起弃用,推荐使用 wp_signon()。
- 功能:检查用户名和密码,验证成功返回 true,失败返回 false。
- 错误处理:通过全局变量 $error 获取登录失败原因,插件扩展时应设置此变量。
- 参数:$username(必需,字符串),$password(必需,字符串),$deprecated(必需,字符串,未使用)。
- 返回值:布尔值,成功为 true,失败为 false。
代码示例
function wp_login(
$username,
#[SensitiveParameter]
$password,
$deprecated = ''
) {
_deprecated_function( __FUNCTION__, '2.5.0', 'wp_signon()' );
global $error;
$user = wp_authenticate($username, $password);
if ( ! is_wp_error($user) )
return true;
$error = $user->get_error_message();
return false;
}注意事项
- 如果用户名为空,全局变量 $error 可能不会被设置,需假设为空用户名情况。
- 插件扩展此函数时,应提供全局 $error 并设置错误信息,以便后续检查。
- 相关函数:wp_authenticate() 用于验证登录凭据,_deprecated_function() 标记弃用,is_wp_error() 检查错误对象。
原文内容
Checks a users login information and logs them in if it checks out. This function is deprecated.
Description
Use the global $error to get the reason why the login failed. If the username is blank, no error will be set, so assume blank username on that case.
Plugins extending this function should also provide the global $error and set what the error is, so that those checking the global for why there was a failure can utilize it later.
See also
Parameters
$usernamestringrequired-
User’s username
$passwordstringrequired-
User’s password
$deprecatedstringrequired-
Not used
Source
function wp_login(
$username,
#[SensitiveParameter]
$password,
$deprecated = ''
) {
_deprecated_function( __FUNCTION__, '2.5.0', 'wp_signon()' );
global $error;
$user = wp_authenticate($username, $password);
if ( ! is_wp_error($user) )
return true;
$error = $user->get_error_message();
return false;
}
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Deprecated. Use wp_signon() |
| 1.2.2 | Introduced. |