WordPress at a glance

username_exists() WP 2.0.0

Checks whether the given username (login) exists.

Checks the presence of the specified username (login) in the user's database. The search passes through the user_login field.

The function requires file:

require_once ABSPATH . WPINC . '/user.php';

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Works based on: get_user_by()
Hooks from the function
Return

Int/false. The user's ID on success, and false on failure.

Usage

username_exists( $username );
$username(string) (required)
Username.

Examples

#1 Example of username verification when registering a new user:

$username = $_POST['username'];
if ( username_exists( $username ) )
	echo 'Username is already in use!';
else
	echo 'You can use this username!';

Changelog

Since 2.0.0 Introduced.

Code of username exists: wp-includes/user.php WP 5.2.4

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

	/**
	 * Filters whether the given username exists or not.
	 *
	 * @since 4.9.0
	 *
	 * @param int|false $user_id  The user's ID on success, and false on failure.
	 * @param string    $username Username to check.
	 */
	return apply_filters( 'username_exists', $user_id, $username );
}

Related Functions

From category: Other

No comments
    Hello, !     Log In . Register