noptin_generate_user_name( string $prefix = '' )

Generates a unique username for new users.


Parameters Parameters

$prefix

(string) (Optional) The prefix to use for the generated user name.

Default value: ''


Top ↑

Return Return

(string.)


Top ↑

Source Source

File: includes/subscriber.php

function noptin_generate_user_name( $prefix = '' ) {

	// If prefix is an email, retrieve the part before the email.
	$prefix = strtolower( trim( strtok( $prefix, '@' ) ) );

	// Remove whitespace.
	$prefix = preg_replace( '|\s+|', '_', $prefix );

	// Trim to 8 characters max.
	$prefix = sanitize_user( $prefix );

	// phpcs:ignore Generic.Commenting.DocComment.MissingShort
	/** @ignore */
	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
	if ( empty( $prefix ) || in_array( strtolower( $prefix ), array_map( 'strtolower', $illegal_logins ), true ) ) {
		$prefix = 'noptin';
	}

	$username = $prefix;
	if ( username_exists( $username ) ) {
		$prefix = $prefix . zeroise( wp_rand( 0, 9999 ), 4 );
		return noptin_generate_user_name( $prefix );
	}

	/**
	 * Filters an autogenerated user_name.
	 *
	 * @since 1.2.3
	 * @param string $prefix      A prefix for the user name. Can be any string including an email address.
	 */
	return apply_filters( 'noptin_generate_user_name', $prefix );
}


Top ↑

Changelog Changelog

Changelog
VersionDescription
1.2.3Introduced.

Top ↑

User Contributed Notes User Contributed Notes

30 day money back guarantee
Safe & Secure online payment
Custom CSS Slide Example
Custom Animation Example
Default Animation