generate unique number when registering a user

This will do what you want, and both functions should be placed within your fucntions.php file.

The my_random_string() function accepts arguments, so you can add data before/after the string, as well as change the length of the string and the characters that are used to generate the string.

/**
 * Generate a string of random characters
 *
 * @param array $args   The arguments to use for this function
 * @return string|null  The random string generated by this function (only 'if($args['echo'] === false)')
 */
function my_random_string($args = array()){
    
    $defaults = array(  // Set some defaults for the function to use
        'characters'    => '0123456789',
        'length'        => 10,
        'before'        => '',
        'after'         => '',
        'echo'          => false
    );
    $args = wp_parse_args($args, $defaults);    // Parse the args passed by the user with the defualts to generate a final '$args' array
    
    if(absint($args['length']) < 1) // Ensure that the length is valid
        return;
    
    $characters_count = strlen($args['characters']);    // Check how many characters the random string is to be assembled from
    for($i = 0; $i <= $args['length']; $i++) :          // Generate a random character for each of '$args['length']'
    
        $start = mt_rand(0, $characters_count);
        $random_string.= substr($args['characters'], $start, 1);
        
    endfor;
    
    $random_string = $args['before'] . $random_string . $args['after']; // Add the before and after strings to the random string
    
    if($args['echo']) : // Check if the random string shoule be output or returned
        echo $random_string;
    else :
        return $random_string;
    endif;
    
}

Here you have the my_on_user_register() function, which is hooked whenever a new user is generated and adds an entry into the wp_usermeta table against the random_number key, but obviously you can change the name of this key as required.

I’d also recommend that you take a look at the Codex for the user_register action.

/**
 * Upon user registration, generate a random number and add this to the usermeta table
 *
 * @param required integer $user_id The ID of the newly registerd user
 */
add_action('user_register', 'my_on_user_register');
function my_on_user_register($user_id){

    $args = array(
        'length'    => 6,
        'before'    => date("Y")
    );
    $random_number = my_random_string($args);
    update_user_meta($user_id, 'random_number', $random_number);

}

Edit

As per your comment, the callback function my_on_user_register() will now generate a number that starts with the current year and then ends with a random 6 character string (of only numbers).

You can also use the below my_extra_user_profile_fields() callback function to output the random number on the users profile page. Note however that this code does not allow the user to edit that number.

/**
 * Output additional data to the users profile page
 *
 * @param WP_User $user Object properties for the current user that is being displayed
 */
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){
    
    $random_number = get_the_author_meta('random_number', $user->ID);
?>
    <h3><?php _e('Custom Properties'); ?></h3>
    
    <table class="form-table">
        <tr>
            <th><label for="address"><?php _e('Random Number'); ?></label></th>
            <td><?php echo $random_number; ?></td>
        </tr>
    </table>
<?php
}

Leave a Comment