0

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?

4
  • 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? Commented Sep 20, 2012 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
    Commented Sep 20, 2012 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
    Commented Sep 20, 2012 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 :-) Commented Sep 20, 2012 at 12:03

2 Answers 2

1

Here is some code that reads the highest number from the user meta table and then adds +1 to that number. This saves that number for each newly 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_user_meta( $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. i.e:

echo "Your userid: PAY-"+$userid.
0
0

Here is a better solution:

add_filter("user_register", "get_unique");
function get_unique($user_id){
    $prefix = "PAY-";
    do {
        $unique = mt_rand();
        $unique = substr($unique, 0, 3);
        $unique = $prefix . $unique;
    } while (!check_unique($unique));
    return update_usermeta( $user_id, 'memnumber', $unique );//$unique;
}
function check_unique($unique) {
    global $wpdb;
    $result = $wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='memnumber' AND meta_value = '$unique'");
    if(empty($result))
        return true;
    return false;
}
1
  • 2
    Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Commented Sep 27, 2014 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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