First off I found this question: generate unique number when registering a user

which is almost exactly what I need except I need the number generated to be auto incremented instead of random. I'd like the first number to be a 4 digit number then auto incremented from there. Ideally it would never get longer than say 6 digits and then would replace the first digit with a letter but maybe thats not possible?

The number needs to be displayed and searchable from the user list in the admin area

//create unique id for decal on new user creation
add_action( 'user_register', 'my_on_user_register' );
function my_on_user_register( $user_id ) {
    $unique_id = 1000 + $user_id;
    update_user_meta( $user_id, 'my_unique_id', $unique_id );
}
//display decal code on profile
add_action('show_user_profile', 'my_extra_user_profile_fields');
add_action('edit_user_profile', 'my_extra_user_profile_fields');
function my_extra_user_profile_fields($user){

    $unique_number = get_the_author_meta('my_unique_id', $user->ID);
?>
    <h3><?php _e('Decal Info'); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="address"><?php _e('Code:'); ?></label></th>
            <td><?php echo $unique_number; ?></td>
        </tr>
    </table>
<?php
}
//send email with decal code to admin on new user
function registration_email_alert($user_id) {
    $message = strip_tags($_POST['user_login']). ' - ' . strip_tags($_POST['user_email']) . ' Decal Code: ' . strip_tags($user_id['my_unique_id']) . ' Has Registered To Your Website';
    wp_mail( 'staff@howsmyteendoing.com', 'New Teen Has Been Registered', $message );
}
add_action('user_register', 'registration_email_alert');
//add decal code to user list
function new_modify_user_table( $column ) {
    $column['my_unique_id'] = 'Decal Code';
    return $column;
}
add_filter( 'manage_users_columns', 'new_modify_user_table' );

function new_modify_user_table_row( $val, $column_name, $user_id ) {
    $user = get_userdata( $user_id );
    switch ($column_name) {
        case 'my_unique_id' :
            return get_the_author_meta( 'my_unique_id', $user_id );
            break;
        default:
    }
    return $return;
}
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );
up vote 0 down vote accepted

You can use the same method as described in the link you mentioned, but simply use the created user ID and add 1000 so you get four digits. So user_id = 5 gets the meta number of 1005.

add_action( 'user_register', 'my_on_user_register' );
function my_on_user_register( $user_id ) {
    $unique_id = 1000 + $user_id;
    update_user_meta( $user_id, 'my_unique_id', $unique_id );
}

Hope that helps!

  • worked a trick thanks. I also added the field to the profile page and to the user list. But any ideas how to make it searchable in the user list? adding my full code to original question – mcmasterp May 7 '15 at 23:57

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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