php – Need help with generating a unique reference number based on the user id

I’m trying to create a unique reference ID based on the actual user ID so I don’t have to reveal the actual user ID on my site. This number will be published later using a shortcode. It is necessary to hide the real user ID and real name due to the privacy policy of my country. I was thinking of a user meta that is automatically generated after registration based on the actual user ID with a specific pattern:

  • 8-digits
  • contains the actual user ID
  • automatically completes the remaining digits with randomly generated numbers for fixing number length
{user_id}{generated_nr}, max 8-digits

I started very simply as follows, but I can’t set the length/width of the number considering the user id, so the missing digits are filled with auto-generated numbers. I also do not know how to generate the number only for newly registered users, nor do I know how to check if the number has already been assigned to another user:

function add_custom_ref_id( $user_id ) {

 $custom_ref_id = $user_id . wp_rand(10000000,99999999);
 return $custom_ref_id;


add_user_meta( $user_id, 'custom_ref_id', $custom_ref_id );

Concerns: Will the numbers be unique? Is this method secure? How can I avoid mistakes? Maybe combining them with the first letters of their first and last name? Like;

Name: John Doe, User ID: 24 > 24XXXXXXJD

And if yes, how?

I am always open to new suggestions and thank you in advance for proposing solutions.

Best regards.

PS: I already read the older threads but apart from the fact that they do not really meet my expectations, there could be better, more secure and more efficient methods out there.