WordPress用户登录函数:wp_signon

WordPress用户登录函数:wp_signon

wp_signon

使用记住用户进行身份登录。

wp_signon( array $credentials = array(), string|bool $secure_cookie = '' )

参数

参数类型含义
$credentials数组|必选用户登录凭据
$secure_cookie字符串|bool|可选是否使用安全的cookie

源码

文件:wp-includes/user.php 33行

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;
}

付费内容

  1. 解释参数 $credentials
  2. 登录示例

为TA充电
共{{data.count}}人
人已赞赏
函数文档

WordPress禁用某个插件:deactivate_plugins

2021-9-14 21:29:38

主题开发

WordPress主题开发:首页

2021-5-25 8:35:45

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索