Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
username_exists() - 确定给定用户名是否存在 - WordPress函数 - WordPress动力
当前浏览:首页 / WordPress函数 / username_exists()

username_exists( string $username )

确定给定用户名是否存在


Warning: Undefined array key -1 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 185

Warning: Undefined array key 2 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 186

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "2df702d60daa556bbc409461c8106fdb" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "38f3621662d06fdb325166bc93aaf42a" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

exists 判断存在


描述

更多类似的主题函数信息,请查看主题开发手册中的条件标签文章。


参数

$username

(string) (必填) 用于检查是否存在的用户名。


返回

(int|false) 成功时返回用户ID,失败时为false。



源码

查看源码 官方文档


更新日志

版本描述
2.0.0开始引入

使用示例

  • 示例1

    示例
    在脚本中使用username_exists()来确定给定的用户名是否存在。

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

    示例
    此函数首先检查用户名是否存在,如果不存在,则创建用户。

    function wpdocs_get_user_id( $username ) {
    	$user_id = username_exists( $username );
    
    	if ( ! $user_id ) {
    		$userdata = array(
    			'user_login' => $username,
    			'user_pass'  => null,
    		);
    
    		$user_id = wp_insert_user( $userdata );
    	}
    
    	return $user_id;
    }