返回介绍

wp_authenticate()

发布于 2017-09-11 11:33:54 字数 3412 浏览 937 评论 0 收藏 0

wp_authenticate( string $username,  string $password )

Authenticate a user, confirming the login credentials are valid.


description


参数

$username

(string) (Required) User's username or email address.

$password

(string) (Required) User's password.


返回值

(WP_User|WP_Error) WP_User object if the credentials are valid, otherwise WP_Error.


源代码

File: wp-includes/pluggable.php

function wp_authenticate($username, $password) {
	$username = sanitize_user($username);
	$password = trim($password);

	/**
	 * Filters whether a set of user login credentials are valid.
	 *
	 * A WP_User object is returned if the credentials authenticate a user.
	 * WP_Error or null otherwise.
	 *
	 * @since 2.8.0
	 * @since 4.5.0 `$username` now accepts an email address.
	 *
	 * @param null|WP_User|WP_Error $user     WP_User if the user is authenticated.
	 *                                        WP_Error or null otherwise.
	 * @param string                $username Username or email address.
	 * @param string                $password User password
	 */
	$user = apply_filters( 'authenticate', null, $username, $password );

	if ( $user == null ) {
		// TODO what should the error message be? (Or would these even happen?)
		// Only needed if all authentication handlers fail to return anything.
		$user = new WP_Error( 'authentication_failed', __( '<strong>ERROR</strong>: Invalid username, email address or incorrect password.' ) );
	}

	$ignore_codes = array('empty_username', 'empty_password');

	if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
		/**
		 * Fires after a user login has failed.
		 *
		 * @since 2.5.0
		 * @since 4.5.0 The value of `$username` can now be an email address.
		 *
		 * @param string $username Username or email address.
		 */
		do_action( 'wp_login_failed', $username );
	}

	return $user;
}

更新日志

Versiondescription
4.5.0$username now accepts an email address.
2.5.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: sanitize_user()
  • wp-includes/pluggable.php: authenticate
  • wp-includes/pluggable.php: wp_login_failed
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/plugin.php: do_action()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 3 more uses Hide more uses

Used By

  • wp-includes/deprecated.php: user_pass_ok()
  • wp-includes/user.php: wp_signon()
  • wp-includes/pluggable-deprecated.php: wp_login()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::login()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文