wc_rand_hash()WC 2.4.0

Generate a rand hash.

No Hooks.

Returns

String.

Usage

wc_rand_hash( $prefix, $max_length );
$prefix(string)
Prefix for the hash.
Default: ''
$max_length(?int)
Maximum length of the hash. Excludes the prefix.
Default: null

Changelog

Since 2.4.0 Introduced.

wc_rand_hash() code WC 10.8.1

function wc_rand_hash( $prefix = '', $max_length = null ) {
	try {
		$random = bin2hex( random_bytes( 20 ) );
	} catch ( Exception $e ) {
		if ( function_exists( 'wp_fast_hash' ) ) {
			$random = bin2hex( substr( wp_fast_hash( wp_rand() ), -20 ) );
		} else {
			$random = bin2hex( substr( sha1( wp_rand() ), -20 ) );
		}
	}

	if ( $max_length && $max_length > 0 ) {
		$random = substr( $random, 0, $max_length );
	}

	return $prefix . $random;
}