• Resolved shreyal

    (@shreyal)


    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?

    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 1 year, 3 months ago by shreyal.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • @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

    (@shreyal)

    @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?

    @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

    (@shreyal)

    @missveronicatv

    Thanks for helping. (:

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Unique ID Generation’ is closed to new replies.