Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have to create a membership site, but I need to give every user that registers a automatic ID number.

For example,

  • User 2 - has a code - pay-102

  • User 3 - has a code - pay-103

I did a forum search and found this, its pretty much what I am asking - What is the fastest way to generate a unique id number when registering a user

I've got a piece of code that will add it to the user panel in the admin area:

function fb_add_custom_user_profile_fields( $user ) {
?>
<h3><?php _e('Extra Profile Informations', 'themename'); ?></h3>
<table class="form-table">
    <tr>
        <th>
            <label for="memnumber"><?php _e('memnumber', 'themename'); ?>
        </label></th>
        <td>
            <input type="text" name="memnumber" id="memnumber" value="<?php echo esc_attr( get_the_author_meta( 'memnumber', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter your Membership number.', 'themename'); ?></span>
        </td>
    </tr>
</table>
<?php }
function fb_save_custom_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
    return FALSE;
update_usermeta( $user_id, 'memnumber', $_POST['memnumber'] );
}
add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );

How do I populate that field, with an incremental number that is assigned?

share|improve this question
    
Hi Andy.. Not sure why you have to create an additional unique id for each user. Each registered user in WP already has a unique user id? I presume the users should not be able to edit their own user id, since that could create conflicts? –  Lars Koudal Sep 20 '12 at 10:52
    
I should also ask, if that field is populated from the wp_users database, and the 'admin' updated the number to a different one, would that update the database field - –  Andy Sep 20 '12 at 10:53
    
Hi Lars, The customer already has a numbering system for members, he has asked if it could continue in the same format (hopefully, just number change) - I personally don't see the point as its a strange request, but I'd like to know if it can be done, if it will cause any problems etc etc... –  Andy Sep 20 '12 at 10:55
    
Hi Andy. I added an answer with the code you need. If the customer has no particular reason for the request, I would not implement the code. But that is me :-) –  Lars Koudal Sep 20 '12 at 12:03

1 Answer 1

up vote 0 down vote accepted

Here is some code that reads the highest number from the user meta table and then adds +1 to that number and saves that number for each new registered user. The code is triggered via the action hook "user_register".

add_action( 'user_register', 'assignuserid');

function assignuserid($user_id) {
   global $wpdb;
   $latestid=$wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='memnumber' order by meta_value DESC limit 1;");
update_usermeta( $user_id, 'memnumber', $latestid+1 );
}

Note: If the "pay-" is fixed, it does not need to be part of the code that stores the value, only part of the output. Ie:

echo "Your userid: PAY-"+$userid.
share|improve this answer
    
Hi Lars, absolutely brilliant, I really really appreciate that, worked straight away!... can I paypal you some money (can't afford a lot)? or donate to a charity of choice? thank you once again. –  Andy Sep 20 '12 at 12:49
    
Do I just press the 'tick' or is there something else I need to do to mark 'answered'? –  Andy Sep 20 '12 at 12:50
    
Hi Andy. My pleasure, glad I could help :-) Forget the money, but thanks for offering. Have a nice day. –  Lars Koudal Sep 20 '12 at 13:25
    
Hi Lars, I am glad you took the time to help me out - will still donate some money to charity... (I try do that once a week anyway)...Once again, so so many thanks. –  Andy Sep 20 '12 at 16:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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