0

When a user registers, I need to assign a random but unique, 4-digit number as user meta data. The number will be static and not change: Not by the user himself or any user with a higher capability. This number/meta data would be needed to be displayed when a user visits a specific page (post type: Page).

  • Connor, I edited your question including the tags. Take a look at it, in case refine it and take the question and the new tags as starting point for your own research. Then come back, file a new edit and show us what you came up with and where you got stuck. – kaiser Feb 28 '14 at 21:15
  • @kaiser It seems like you understand my question very well. I'm glad your trying to help but revising my question doesn't actually help. If i knew how to assign random user meta data upon registration, i wouldn't have came on here asking how to do it.. asking people online IS how you research – Conner Feb 28 '14 at 21:41
  • Well, we got tag archives: users, user-meta, etc. and you can use [users] random meta as search string that only searches a specific tag archive for example. And yes, it took me quite some time to figure out what you're trying to do. – kaiser Feb 28 '14 at 21:46
2

You can use the hook user_register to modify user data upon registration. You can use the function add_user_meta to add custom data to a user. Example:

add_action('user_register', 'add_pin_number', 10, 1);

function add_pin_number($user_id)
{

   add_user_meta( $user_id, 'pin_number', $random_number , true );

}

You can look for php function out there to help you creating the random unique number. One idea is to create the number based on the user ID which is already unique.

  • +1 Perfect answer. The only thing that would be missing to make it overly complete: Checking the length of the ID and prepending zeros for shorter IDs. And: Longer IDs would need handling as well. When a user registers and deletes his account again, you would miss out a number, etc. But that's the only flaw of that concept. Maybe just updating a single options entry with the currently highest number (just ++ the old value and save it as a new one upon registration) would be simpler. – kaiser Feb 28 '14 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.