Title: Unique ID Generation
Last modified: August 21, 2023

---

# Unique ID Generation

 *  Resolved [shreyal](https://wordpress.org/support/users/shreyal/)
 * (@shreyal)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/unique-id-generation/)
 * In this code, the user is getting a unique code with a prefix “C”, I want to 
   add a condition in it, if a person selects their gender as “Male”, the Unique
   ID should have prefix “CM”, and if selects “Female”, it should be “CF”. How can
   I achieve this?
 *     ```wp-block-code
       add_action("user_register","um_012022_generate_unique_account_id");
       function um_012022_generate_unique_account_id( $user_id ) {
          $unique_account_id = 'C-' . str_pad( $user_id, 5, '0', STR_PAD_LEFT );
          update_user_meta( $user_id, "um_unique_account_id", $unique_account_id );
       }
       ```
   
 * Thank you,
    -  This topic was modified 2 years, 11 months ago by [shreyal](https://wordpress.org/support/users/shreyal/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Funique-id-generation%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [missveronica](https://wordpress.org/support/users/missveronicatv/)
 * (@missveronicatv)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/unique-id-generation/#post-16989391)
 * [@shreyal](https://wordpress.org/support/users/shreyal/)
 * You can try to replace your current code snippet with this code snippet
    for 
   prefix of CF- or CM- depending on the user selected `gender`
 *     ```
       add_action( "um_user_register","um_012022_generate_unique_account_id", -1, 2 );
       function um_012022_generate_unique_account_id( $user_id, $args ) {
   
           $prefix = 'C-';
           if ( isset( $args['gender'] ) && ! empty( $args['gender'][0] )) {
               if ( $args['gender'][0] == 'Female') {
                   $prefix = 'CF-';
               }
               if ( $args['gender'][0] == 'Male') {
                   $prefix = 'CM-';
               }
           }
          $unique_account_id = $prefix . str_pad( $user_id, 5, '0', STR_PAD_LEFT );
          update_user_meta( $user_id, "um_unique_account_id", $unique_account_id );
       }
       ```
   
 *  Thread Starter [shreyal](https://wordpress.org/support/users/shreyal/)
 * (@shreyal)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/unique-id-generation/#post-16993440)
 * [@missveronicatv](https://wordpress.org/support/users/missveronicatv/)
 * Thanks a lot for the help! It is working fine. (:
 * Can you tell me how I can make the generated Unique ID the username of the person
   who registers?
 *  [missveronica](https://wordpress.org/support/users/missveronicatv/)
 * (@missveronicatv)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/unique-id-generation/#post-16993707)
 * [@shreyal](https://wordpress.org/support/users/shreyal/)
 * You can try to replace your current code snippet with this code snippet.
 *     ```
       add_action( "um_before_save_registration_details", "um_012022_generate_unique_account_id", 10, 3 );
       function um_012022_generate_unique_account_id( $user_id, $args, $form_data ) {
   
           global $wpdb;
   
           $prefix = 'C-';
           if ( isset( $args['gender'] ) && ! empty( $args['gender'][0] )) {
               if ( $args['gender'][0] == 'Female') {
                   $prefix = 'CF-';
               }
               if ( $args['gender'][0] == 'Male') {
                   $prefix = 'CM-';
               }
           }
           $unique_account_id = $prefix . str_pad( $user_id, 5, '0', STR_PAD_LEFT );
           $wpdb -> update( $wpdb -> users, 
                               array( 'user_login' => $unique_account_id ), 
                               array( 'ID' => $user_id ) 
                           );
           //update_user_meta( $user_id, "um_unique_account_id", $unique_account_id );
           UM()->user()->remove_cache( $user_id );
           um_fetch_user( $user_id );   
       }
       ```
   
 *  Thread Starter [shreyal](https://wordpress.org/support/users/shreyal/)
 * (@shreyal)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/unique-id-generation/#post-16996730)
 * [@missveronicatv](https://wordpress.org/support/users/missveronicatv/)
 * Thanks for helping. (:

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Unique ID Generation’ is closed to new replies.

 * ![](https://ps.w.org/ultimate-member/assets/icon-256x256.png?rev=3160947)
 * [Ultimate Member – User Profile, Registration, Login, Member Directory, Content Restriction & Membership Plugin](https://wordpress.org/plugins/ultimate-member/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ultimate-member/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ultimate-member/)
 * [Active Topics](https://wordpress.org/support/plugin/ultimate-member/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ultimate-member/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ultimate-member/reviews/)

## Tags

 * [Registration](https://wordpress.org/support/topic-tag/registration/)

 * 4 replies
 * 2 participants
 * Last reply from: [shreyal](https://wordpress.org/support/users/shreyal/)
 * Last activity: [2 years, 11 months ago](https://wordpress.org/support/topic/unique-id-generation/#post-16996730)
 * Status: resolved