wp_signon

函数
wp_signon ( $credentials = array(), $secure_cookie = '' )
参数
  • (array) $credentials Optional. User info in order to sign on.
    Required:
    Default: array()
  • (string|bool) $secure_cookie Optional. Whether to use secure cookie.
    Required:
    Default: (empty)
返回值
  • (WP_User|WP_Error) WP_User on success, WP_Error on failure.
定义位置
相关方法
wp_send_jsonwp_site_iconwp_doing_cronwpmu_signup_blogwp_cron
引入
2.5.0
弃用
-

wp_signon是一个函数,用于根据所提供的凭证登录到WordPress网站的用户。

用’remember’权限认证和登录一个用户。

credentials是一个数组,有’user_login’、’user_password’和’remember’索引。如果没有给出凭证,那么将假定并使用已设置的登录表单。

各种认证cookie将被这个函数设置,并将根据’记住’凭证是否被设置为”true”而设置较长的时间。

注意:wp_signon()并不处理设置当前用户。这意味着,如果该函数在{@see ‘init’}钩子启动之前被调用,is_user_logged_in()将在那之前评估为假。如果is_user_logged_in()需要和wp_signon()一起使用,应该明确调用wp_set_current_user()。

function wp_signon( $credentials = array(), $secure_cookie = '' ) {
	if ( empty( $credentials ) ) {
		$credentials = array(); // Back-compat for plugins passing an empty string.

		if ( ! empty( $_POST['log'] ) ) {
			$credentials['user_login'] = wp_unslash( $_POST['log'] );
		}
		if ( ! empty( $_POST['pwd'] ) ) {
			$credentials['user_password'] = $_POST['pwd'];
		}
		if ( ! empty( $_POST['rememberme'] ) ) {
			$credentials['remember'] = $_POST['rememberme'];
		}
	}

	if ( ! empty( $credentials['remember'] ) ) {
		$credentials['remember'] = true;
	} else {
		$credentials['remember'] = false;
	}

	/**
	 * Fires before the user is authenticated.
	 *
	 * The variables passed to the callbacks are passed by reference,
	 * and can be modified by callback functions.
	 *
	 * @since 1.5.1
	 *
	 * @todo Decide whether to deprecate the wp_authenticate action.
	 *
	 * @param string $user_login    Username (passed by reference).
	 * @param string $user_password User password (passed by reference).
	 */
	do_action_ref_array( 'wp_authenticate', array( &$credentials['user_login'], &$credentials['user_password'] ) );

	if ( '' === $secure_cookie ) {
		$secure_cookie = is_ssl();
	}

	/**
	 * Filters whether to use a secure sign-on cookie.
	 *
	 * @since 3.1.0
	 *
	 * @param bool  $secure_cookie Whether to use a secure sign-on cookie.
	 * @param array $credentials {
	 *     Array of entered sign-on data.
	 *
	 *     @type string $user_login    Username.
	 *     @type string $user_password Password entered.
	 *     @type bool   $remember      Whether to 'remember' the user. Increases the time
	 *                                 that the cookie will be kept. Default false.
	 * }
	 */
	$secure_cookie = apply_filters( 'secure_signon_cookie', $secure_cookie, $credentials );

	global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie().
	$auth_secure_cookie = $secure_cookie;

	add_filter( 'authenticate', 'wp_authenticate_cookie', 30, 3 );

	$user = wp_authenticate( $credentials['user_login'], $credentials['user_password'] );

	if ( is_wp_error( $user ) ) {
		return $user;
	}

	wp_set_auth_cookie( $user->ID, $credentials['remember'], $secure_cookie );
	/**
	 * Fires after the user has successfully logged in.
	 *
	 * @since 1.5.0
	 *
	 * @param string  $user_login Username.
	 * @param WP_User $user       WP_User object of the logged-in user.
	 */
	do_action( 'wp_login', $user->user_login, $user );
	return $user;
}

常见问题

FAQs
查看更多 >