WP_REST_Users_Controller::check_username() WordPress Method

The WP_REST_Users_Controller::check_username() method is used to check if a given username is valid and available. This is useful when registering new users or changing your username.

WP_REST_Users_Controller::check_username( string $value, WP_REST_Request $request, string $param ) #

Check a username for the REST API.


Description

Performs a couple of checks like edit_user() in wp-admin/includes/user.php.


Top ↑

Parameters

$value

(string)(Required)The username submitted in the request.

$request

(WP_REST_Request)(Required)Full details about the request.

$param

(string)(Required)The parameter name.


Top ↑

Return

(string|WP_Error) The sanitized username, if valid, otherwise an error.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

	public function check_username( $value, $request, $param ) {
		$username = (string) $value;

		if ( ! validate_username( $username ) ) {
			return new WP_Error(
				'rest_user_invalid_username',
				__( 'This username is invalid because it uses illegal characters. Please enter a valid username.' ),
				array( 'status' => 400 )
			);
		}

		/** This filter is documented in wp-includes/user.php */
		$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );

		if ( in_array( strtolower( $username ), array_map( 'strtolower', $illegal_logins ), true ) ) {
			return new WP_Error(
				'rest_user_invalid_username',
				__( 'Sorry, that username is not allowed.' ),
				array( 'status' => 400 )
			);
		}

		return $username;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.