WordPress函数:username_exists 检查用户名是否已存在

江河/ 2023年07月08日/ WordPress/ 浏览 727

函数原型:


username_exists( string $username ): int|false


确定给定的用户名是否存在。


参数说明:


$username,要检查的用户名。


函数源码:


function username_exists( $username ) {
	$user = get_user_by( 'login', $username );
	if ( $user ) {
		$user_id = $user->ID;
	} else {
		$user_id = false;
	}

	return apply_filters( 'username_exists', $user_id, $username );
}


包含钩子:


apply_filters( 'username_exists', int|false $user_id, string $username )


使用举例:


$username = sanitize_user( $_POST['username'] );
if ( username_exists( $username ) ) {
	echo "Username In Use!";
} else {
	echo "Username Not In Use!";
}


发表评论

暂无评论,抢个沙发...

客服 工单