0

I have a membership site with 4000+ users currently on it.

I am needing to give both new/existing users an automatic ID number.

its very similar to this: Create Unique ID for user

However, this only works for new users, I need it to work for the current users too. So for instance, user id 3421 would get membership number 3421.

This is what I have so far: (pretty much all from above link)

function fb_add_custom_user_profile_fields( $user ) { ?>
        <h3><?php _e('Extra Profile Information', 'Avada'); ?></h3>
            <table class="form-table">
                <tr>
                    <th>
                        <label for="memnumber"><?php _e('Membership Number', 'Avada'); ?>
                        </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 />
                    </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' );




    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 );
}

The solution would need to be safe for the current users.

Any advice would be appreciated.

OverdueOrange
  • 99
  • 1
  • 10
  • Is the member number always going to be the same as the User ID? Does the member number need to be changed once set? Depending on the answers, you might be able to do something much simpler, and foolproof, without cluttering up the profile pages. – CK MacLeod Jun 05 '22 at 19:30
  • @CKMacLeod Its the same as the ID and doesnt need to change. Just needs a prefix of say, "prefix5201" – OverdueOrange Jun 06 '22 at 07:59
  • @CKMacLeod And if it would be easier - just to start from a random number and incremement by 1 every time. S0... prefix1234561, prefix1234562 etc. – OverdueOrange Jun 06 '22 at 08:30
  • I probably should also have asked, once the number is set, where to you need to see it or how to you need to use it - is just having it as user_meta and going from there sufficient? – CK MacLeod Jun 06 '22 at 16:22
  • @CKMacLeod Ideally it would be somewhere an admin could see it within the user profile. – OverdueOrange Jun 07 '22 at 07:30

1 Answers1

0

from the docs, the update_user_meta function returns the 'Meta ID if the key didn't exist, true on successful update, false on failure or if the value passed to the function is the same as the one that is already in the database.'

So something like

<?php

$new_user_num = $_POST['memnumber'];

// Assume existing user memnumber update to their id
$res = update_usermeta( $user_id, 'memnumber', $user_id);


if (! $res) { // New user, memnumber set already or failures
    $create_res = update_usermeta( $user_id, 'memnumber', $new_user_num);
    if (! $create_res)
        return 1;
}
return 0;

?>
Yohan de Rose
  • 134
  • 2
  • 7
  • Actually this line would be enough, as all conditions must be included in the function `update_usermeta` already: `$memnumber = update_usermeta( $user_id, 'memnumber', ($_POST['memnumber'] ?: ''));`. – David Jun 05 '22 at 16:29
  • @David & yohan what will this replace? and how can i add a prefix to the number also? – OverdueOrange Jun 06 '22 at 08:15
  • Replacing the body of the fb_save_custom_user_profile_fields function. In PHP (assuming the prefix is a number), you'll need to convert memnumber to a string then append the prefix as a string, then convert back to an int so: $prefix = 101069; $new_memnum = (int) (strval($prefix) . strval($_POST['memnumber'])); – Yohan de Rose Jun 06 '22 at 09:18
  • Hi @YohandeRose thank you. Last thing, would there be a way to keep incrementing from the last number? from my understanding wouldn't the above code find a $latestid if a user had been deleted? i.e. user with id 4 gets deleted and then a newly created user would automatically get 4 because its the smallest available number? – OverdueOrange Jun 06 '22 at 10:24