2

I need to add a number to user as meta info (it's a 5 digit number) at the time they register. I assume I would use the user_register action to call a script. I also assume I would use update_user_meta to add the number. But I am not sure how to increment it up one for the next user. Would I have to have a lookup table to see what the last one was? Is there away to look at the last members usermeta(member_number) and then add one? What would be the best way to handle this. There currently exists a roster with assigned numbers, which when I import the users they will receive their number. But after that the next member needs to start at the last number in this roster.

Here is what I tried

    // Add member number at time of registration
global $current_user;
      get_currentuserinfo();

function add_member_no() {
    // just to stick something in the first time.
    $start_no = 507602;
    add_option('member_no', $start_no);

    $last_member_no = get_option(member_no);
    $last_member_no += $last_member_no;
    update_option('member_no', $last_member_no);

    add_user_meta($current_user->id, 'membership_no', $last_member_no);
}
add_action( 'user_register', 'add_member_no');
0

1 Answer 1

1

To get the ID of the user that was created last, you could run the following query:

SELECT id FROM wp_users ORDER BY user_registered DESC LIMIT 1

To do this within the context of your code, do this:

global $wpdb;
$last_user_id = $wpdb->get_results("SELECT id FROM $wpdb->users ORDER BY user_registered DESC LIMIT 1");

That will give you access to an instance of the WordPress query object and it will properly the query the database regardless of the table prefix.

You can print_r the result to check the result.

5
  • Thanks Tom. One more thing When I take the next step to add one. global $wpdb; $last_user_id = $wpdb->get_results("SELECT id FROM $wpdb->users ORDER BY user_registered DESC LIMIT 1"); $new_user_id = $last_user_id + 1; I get this error: Fatal error: Unsupported operand types Aug 14, 2011 at 15:38
  • Ok I see it is giving back an array, but I also noticed it is giving back the last user ID (example: 18) but not the next user ID. What I mean by this, is if several users have been deleted the next number would not be 19 but the next conical number. I'm not sure how to get that. Aug 14, 2011 at 15:56
  • If a user is deleted, then that record is deleted from the database so the ID will no longer exist. You can get the ID for the next user by simply adding one to the number returned from the database (if I understand your question correctly).
    – Tom
    Aug 14, 2011 at 16:30
  • Ok here is what I ended up with // Add member number at time of registration add_action( 'user_register', 'add_membership_no'); function add_membership_no( $user_id ) { /* Just to stick something in the first time. $start_no = '507602'; $option_name = 'member_no'; update_option($option_name, $start_no); */ $last_member_no = get_option(member_no); $last_member_no = $last_member_no + 1; update_option('member_no', $last_member_no); add_user_meta($user_id, 'membership_no', $last_member_no); } Aug 14, 2011 at 16:42
  • 1
    Use user_meta data instead of an option per user. That's a) the wrong table and b) cluttering the options table.
    – kaiser
    Nov 14, 2011 at 23:46

Your Answer

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

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