Title: validate_username
Published: April 25, 2014
Last modified: August 20, 2026

---

# validate_username( string $username ): bool

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#wp--skip-link--target)

Checks whether a username is valid.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#parameters)󠁿

 `$username`stringrequired

Username.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#return)󠁿

 bool Whether username given is valid.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#more-information)󠁿

This function attempts to sanitize the username, and if it “passes”, the name is
considered valid. For additional logic, you can use the ‘`validate_username`‘ hook.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#source)󠁿

    ```php
    function validate_username( $username ) {
    	$sanitized = sanitize_user( $username, true );
    	$valid     = ( $sanitized === $username && ! empty( $sanitized ) );

    	/**
    	 * Filters whether the provided username is valid.
    	 *
    	 * @since 2.0.1
    	 *
    	 * @param bool   $valid    Whether given username is valid.
    	 * @param string $username Username to check.
    	 */
    	return apply_filters( 'validate_username', $valid, $username );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/user.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/user.php#L2139)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/user.php#L2139-L2152)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#hooks)󠁿

 [apply_filters( ‘validate_username’, bool $valid, string $username )](https://developer.wordpress.org/reference/hooks/validate_username/)

Filters whether the provided username is valid.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#related)󠁿

| Uses | Description | 
| [sanitize_user()](https://developer.wordpress.org/reference/functions/sanitize_user/)`wp-includes/formatting.php` |

Sanitizes a username, stripping out unsafe characters.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [WP_REST_Users_Controller::check_username()](https://developer.wordpress.org/reference/classes/wp_rest_users_controller/check_username/)`wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php` |

Check a username for the REST API.

  | 
| [edit_user()](https://developer.wordpress.org/reference/functions/edit_user/)`wp-admin/includes/user.php` |

Edit user settings based on contents of $_POST

  | 
| [register_new_user()](https://developer.wordpress.org/reference/functions/register_new_user/)`wp-includes/user.php` |

Handles registering a new user.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.4.0](https://developer.wordpress.org/reference/since/4.4.0/) | Empty sanitized usernames are now considered invalid. | 
| [2.0.1](https://developer.wordpress.org/reference/since/2.0.1/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/validate_username/?output_format=md#comment-content-1365)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/validate_username/#comment-1365)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fvalidate_username%2F%23comment-1365)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fvalidate_username%2F%23comment-1365)
 4. **Example **
 5.     ```php
        if ( ! empty( $_POST['username'] ) && validate_username( $_POST['username'] ) ) {
        	// Go ahead and save the user...
        }
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fvalidate_username%2F%3Freplytocom%3D1365%23feedback-editor-1365)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fvalidate_username%2F)
before being able to contribute a note or feedback.