wp_authenticate_username_password() - WordPress 函数参考中文文档

返回介绍

wp_authenticate_username_password()

发布于 2017-09-11 字数3459 浏览 1011 评论 0

wp_authenticate_username_password( WP_User|WP_Error|null $user,  string $username,  string $password )

Authenticate a user, confirming the username and password are valid.


描述


参数

$user

(WP_User|WP_Error|null)
(Required)
WP_User or WP_Error object from a previous callback. Default null.

$username

(string)
(Required)
Username for authentication.

$password

(string)
(Required)
Password for authentication.


返回值

(WP_User|WP_Error) WP_User on success, WP_Error on failure.


源代码

File: wp-includes/user.php

function wp_authenticate_username_password($user, $username, $password) {
	if ( $user instanceof WP_User ) {
		return $user;
	}

	if ( empty($username) || empty($password) ) {
		if ( is_wp_error( $user ) )
			return $user;

		$error = new WP_Error();

		if ( empty($username) )
			$error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.'));

		if ( empty($password) )
			$error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));

		return $error;
	}

	$user = get_user_by('login', $username);

	if ( !$user ) {
		return new WP_Error( 'invalid_username',
			__( '<strong>ERROR</strong>: Invalid username.' ) .
			' <a href="' . wp_lostpassword_url() . '">' .
			__( 'Lost your password?' ) .
			'</a>'
		);
	}

	/**
	 * Filters whether the given user can be authenticated with the provided $password.
	 *
	 * @since 2.5.0
	 *
	 * @param WP_User|WP_Error $user     WP_User or WP_Error object if a previous
	 *                                   callback failed authentication.
	 * @param string           $password Password to check against the user.
	 */
	$user = apply_filters( 'wp_authenticate_user', $user, $password );
	if ( is_wp_error($user) )
		return $user;

	if ( ! wp_check_password( $password, $user->user_pass, $user->ID ) ) {
		return new WP_Error( 'incorrect_password',
			sprintf(
				/* translators: %s: user name */
				__( '<strong>ERROR</strong>: The password you entered for the username %s is incorrect.' ),
				'<strong>' . $username . '</strong>'
			) .
			' <a href="' . wp_lostpassword_url() . '">' .
			__( 'Lost your password?' ) .
			'</a>'
		);
	}

	return $user;
}

更新日志

Version 描述
2.8.0 Introduced.

相关函数

Uses

  • wp-includes/l10n.php:
    __()
  • wp-includes/pluggable.php:
    wp_check_password()
  • wp-includes/pluggable.php:
    get_user_by()
  • wp-includes/general-template.php:
    wp_lostpassword_url()
  • wp-includes/plugin.php:
    apply_filters()
  • wp-includes/user.php:
    wp_authenticate_user
  • wp-includes/load.php:
    is_wp_error()
  • wp-includes/class-wp-error.php:
    WP_Error::__construct()
  • Show 3 more uses
    Hide more uses


User Contributed Notes

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!