validate_username example


    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 ) ) {
            

    $user_email = apply_filters( 'user_registration_email', $user_email );

    // Check the username.     if ( '' === $sanitized_user_login ) {
        $errors->add( 'empty_username', __( '<strong>Error:</strong> Please enter a username.' ) );
    } elseif ( ! validate_username( $user_login ) ) {
        $errors->add( 'invalid_username', __( '<strong>Error:</strong> This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
        $sanitized_user_login = '';
    } elseif ( username_exists( $sanitized_user_login ) ) {
        $errors->add( 'username_exists', __( '<strong>Error:</strong> This username is already registered. Please choose another one.' ) );
    } else {
        /** This filter is documented in wp-includes/user.php */
        $illegal_user_logins = (array) apply_filters( 'illegal_user_logins', array() );
        if ( in_array( strtolower( $sanitized_user_login )array_map( 'strtolower', $illegal_user_logins ), true ) ) {
            $errors->add( 'invalid_username', __( '<strong>Error:</strong> Sorry, that username is not allowed.' ) );
        }
    }

    


    // Checking the password has been typed twice the same.     if ( ( $update || ! empty( $pass1 ) ) && $pass1 !== $pass2 ) {
        $errors->add( 'pass', __( '<strong>Error:</strong> Passwords do not match. Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) );
    }

    if ( ! empty( $pass1 ) ) {
        $user->user_pass = $pass1;
    }

    if ( ! $update && isset( $_POST['user_login'] ) && ! validate_username( $_POST['user_login'] ) ) {
        $errors->add( 'user_login', __( '<strong>Error:</strong> This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
    }

    if ( ! $update && username_exists( $user->user_login ) ) {
        $errors->add( 'user_login', __( '<strong>Error:</strong> This username is already registered. Please choose another one.' ) );
    }

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

    if ( in_array( strtolower( $user->user_login )array_map( 'strtolower', $illegal_logins ), true ) ) {
        
Home | Imprint | This part of the site doesn't use cookies.