0

I know there are userID, username in the database, but I want a formatted and unique number for physical world use. For example: "2014xxxx" 201414:year,xxxx:generate randomly when registering a user.

Is it possible and if possible, what is the simplest way and fastest way to do so?

  • Why do you want that and how do you plan to use this unique number? – sakibmoon Jun 15 '15 at 7:18
0

This way would work ... the user_register action is fired when the user is stored in the data base. You can add meta data to a user with add_metadata.

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

function myplugin_registration_save( $user_id ) {

    add_metadata( $user_id, 'unique_number', date( 'Y' ).str_pad( $user_id, 4 ) );

}
  • 1
    Use add_user_meta instead of add_metadata. Also I believe unique key should be set to true in this case. – sakibmoon Jun 15 '15 at 7:33
  • @skibmoon, eh ... yes. Sorry :) – ChrisL Jun 15 '15 at 7:38
  • Should i paste this code to my function.php ? – Amit Patra Jun 15 '15 at 7:58
  • If you don't want to wrap it in a Plugin, you need to paste that code to your functions.php. But please consider @sakibmoon's comment. – ChrisL Jun 15 '15 at 8:10
  • I just added add_action( 'user_register', 'myplugin_registration_save', 10, 1 ); function myplugin_registration_save( $user_id ) { add_user_meta( $user_id, 'unique_number', date( 'Y' ).str_pad( $user_id, 4 ) ); } – Amit Patra Jun 15 '15 at 8:28

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.