BuddyDev

Search

Custom User ID Custom created

  • Participant
    Level: Initiated
    Posts: 4
    Curious on #47023

    Hello,
    I want a Feature which can allow Custom User ID creation at Registration.

    This will be different from the User ID that WordPress or WooCommerce creates at Registration.

    This one is something that is Custom Created and Custom determined by Site Admin.

    It will have a Prefix already determined by Admin, and the numbering system would also be pre-determined by Admin as well.

    It should be treated as a Custom Post Type that will be on the WordPress Menu. And should have a Shortcode that can allow it to be displayed anywhere on Posts, Pages, and Widget areas in WordPress.

    This Custom ID Feature will have the following Parameters:

    1.) ID TITLE: This is the Name that Admin will call the Custom ID. And Admin should be able to create Multiple Types of Custom IDs according to User Type and User Role.

    2.) ID PREFIX: A Prefix that will appear, separated by Comma, Dash, Pipe, or any other separator.

    3.) NUMBER FORMAT: This is the Format in which the Numbers are applied. It can be numeric, starting from 1 and upwards. Or Admin can set it to have leading Zeros like “000” before the counting starts.

    4.) BATCH NUMBER RANGE: This is the Number of Users that will form a Batch, before they are split into a new batch.

    This range can be 1 to 1,000(One Thousand), 10,000(Ten Thousand) and whatever else the Admin decides.

    5.)BATCH ID TYPE: This is the Grouping of Users into Batches, and the Type of Data Type used in this Grouping.

    It could be– A to Z, Alphanumeric, Symbol, or Text.

    6.) OUTPUT: When these are now pre-determined by Admin, and a User Registers on the website, a Custom User ID should be automatically generated during User Registration, and should be added to the WordPress Dashboard Menu, as “User ID” or whatever Admin has already prenamed it, as seen here– https://prnt.sc/o07lm-UVtkFa

    So in this Case, as seen from the Image above, Admin has called the Custom ID “Tag Number“.

    He has used the Prefix “SP“.

    He has used Number format with two leading Zeros “00“, and we can see that the current User is Number 19.

    The User’s Batch Number is 1, where Batch Number is represented by “BN“.

    I want to believe that by now, you understand what I am talking about?

    So I am asking– do you have any plugin that can do something like that for me?

    Or, do you know of any PHP Code Snippets or WordPress plugins that can do that for me?

    Regards.

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #47024

    Hi,
    Thank you for the question.

    What you are looking is not available as a plugin. If it was about simply generating a user nice name/user id in certain specific format, that could be easily achieved.

    since you need shortcodes and other things(like post type menu and so on), You will need to custom code it.

    You can hook to ‘user_register’ to generate your entries and then add them in admin menu as you need.

    This will need more than an hour to achieve everything you have mentioned and is beyond what we can offer as support here.

    If you are comfortable with php, please look for registering post type, using wp_insert_post and ‘user_register’ action hook. That should give you the pointers.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Curious on #47025

    Your reply did make my day.Your reply made me very happy.

    Even though it has not provided my solution, it did give me hope somehow that there could be a light at the end of the tunnel.

    Now, I want the IDs to be automatically generated. See here– https://wordpress.stackexchange.com/questions/65738/create-unique-id-for-user

    Somehow, I believe that a Code from that website could be a Key. I mean this Code below:

    
    add_filter("user_register", "get_unique");
    function get_unique($user_id){
        $prefix = "PAY-";
        do {
            $unique = mt_rand();
            $unique = substr($unique, 0, 3);
            $unique = $prefix . $unique;
        } while (!check_unique($unique));
        return update_usermeta( $user_id, 'memnumber', $unique );//$unique;
    }
    function check_unique($unique) {
        global $wpdb;
        $result = $wpdb->get_var("SELECT meta_value from $wpdb->usermeta where meta_key='memnumber' AND meta_value = '$unique'");
        if(empty($result))
            return true;
        return false;
    }
    

    ============================================================

    The challenge is that I don’t know PHP. I only know how to insert PHP Code Snippets into my Child Theme’s Functions.PHP file.

    Instinctively, I feel that my answer is somewhere in that code.

    QUESTION 1:
    Can you Please help me look to that Page, and look at the Codes there, to see if they can provide my solution?

    My Point is that, what I am looking for, is not impossible. I’ve just not found it yet, in WordPress.

    So, can you Please help me take a look here– https://wordpress.stackexchange.com/questions/65738/create-unique-id-for-user

    —- to see how that Page could provide solution for me?

    Needing to hear from you soon.

    Regards.

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #47029

    Hi,
    I am sorry, I misread your requirement. I had not looked at the screenshot and thought you wanted the menu in WordPress dashboard(wp-admin).

    What you are looking for is a simple task(generate,store configuration based unique ids with user). I have done it in past for a dating site of one our clients(generated id based on gender field, a predefined prefix and so on).

    I am not sure which theme are you using, so I may not be able to put the data there(Though, I am assuming that is WooCommerce my account page and should work).

    Please share a sample configuration and an example user id and I will share the code which will store the generated id and a function to fetch that id.

    Also, you are in the right direction with the stackexchange page.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Curious on #47030

    Oh my goodness, You are heaven sent.

    Now, I will like us to imagine 2 scenarios— a scenario where there will be no WooCommerce on the website.

    And a second scenario where there will be WooCommerce on the website.

    SCENARIO ONE:
    This is a School Alumni Website– where there’s no payment for anything. Past students just register and create Account.

    Then a Unique ID is generated for them, to be shown on their Profile Page.

    SCENARIO TWO:
    A Laundry Business wants its customers to Register through WooCommerce My-Account Page.

    Then when the Customers have submitted their clothes to the Laundry Business for washing, the Unique ID is the Reference number that the Customer quotes to the company, in order to look for their Clothes. As you can see here– https://prnt.sc/o07lm-UVtkFa

    These are the Two Scenarios. The Numbering Prefix would be different, but the Logic is the same.

    Meaning that, for the Alumni Website, the Prefix is “STN” for Student Number. While for the Laundry Business, the Prefix is “Tag Number”.

    Here is the Numbering Logic:

    Prefix, then number counts from 1 to infinity.

    By the Time we get to the One Millionth Member, the number will be STN-1,000,000.

    By the 1 Bilionth Member, the Number will be too long. So I imagined Batch Number Range– the method of splitting the Site Members or Users into smaller units called “Batches”.

    So, if I set 10,000 as the Batch Range. It means that when I hit Ten Thousand, the Number will be STN-10,000-BN1– with BN1 representing Batch Number 1.

    When the number is 10,001, this one additional person will fall into Batch2, and his Unique ID will be STN-1-BN2.

    This is the Numbering Logic. And I do hope you understand?

    Special Regards.

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #47044

    Hi,
    Thank you for the details. I have been a bit away. will look into it and get back to you in next 24 hours.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 4
    Curious on #47047

    Hello Sir,
    You are such a light. Your willing answers, gave me the courage to try reaching out in other places.

    This seemed to have paid off, in about 90% of my need. This code below has actually achieved what I want:

    `function createCustomUserId($user)
    {
    $user_id = intval($user->data->ID);
    $id_prefix = ‘pie’;
    $separator = ‘-‘;
    $number_format = ‘000’;
    $batch_symbol = ‘BN’;
    $batch_number = ‘1’;

    $custom_id = $id_prefix . $separator . $number_format . $user_id . ‘ | ‘ . $batch_symbol . $batch_number;

    update_user_meta( $user_id, ‘custom_user_id’, $custom_id);
    }
    add_action( ‘pie_register_after_user_created’ , ‘createCustomUserId’, 10, 1);

    function showCustomUserId()
    {
    $output = ”;

    if ( is_user_logged_in() )
    {
    $user_id = get_current_user_id();
    $custom_id = get_user_meta( $user_id , “custom_user_id” , true);

    $output .= ‘<div class=”custom_id_div”>’;
    $output .= ‘<p>’;
    $output .= $custom_id;
    $output .= ‘</p>’;
    $output .= ‘</div>’;

    return $output;
    }
    }
    add_shortcode( ‘custom_user_id’, ‘showCustomUserId’ ); `

    =======================
    And it produced something like this—- https://imgur.com/ONnbaIF

    Now, I have gone through the Code, and I believe that the Batch Range Logic is still missing from the Code.

    Since this code has done 90% the job of what I’m looking for. All I need from you now, is to help provide me the few additional lines of code into this code, that will provide for the Batch Range Logic.

    Meaning that, I am saying– You should please help me rewrite this code above, and add into it, the part that helps the code to check if the number has hit 10,000.

    Then when it is 10,001, it should assign 1 as the number of the 10,001 person, then increase the Batch Number from BN1 to BN2.

    On every 10,000, the Batch Number(BN) should increase sequentially from 1 to 2 to 3, to 4,5,6, on and on and on like that.

    This Batch Range Logic is what I believe is till missing from the code above. And I am hoping you can help me add it in.

    Needing to hear from you soon.

    Regards.

  • Keymaster
    (BuddyDev Team)
    Posts: 24149
    Brajesh Singh on #47054

    Hi,
    Here is your updated code.

    
    
    function createCustomUserId( $user ) {
    	$user_id       = intval( $user->data->ID );
    	$id_prefix     = 'pie';
    	$separator     = '-';
    	$number_format = '000';
    	$batch_symbol  = 'BN';
    	$batch_size    = 10000;
    	$batch_number  = 1 + intval( ( $user_id - 1 ) / $batch_size );
    
    	$custom_id = $id_prefix . $separator . $number_format . $user_id . ' | ' . $batch_symbol . $batch_number;
    
    	update_user_meta( $user_id, 'custom_user_id', $custom_id );
    }
    
    add_action( 'pie_register_after_user_created', 'createCustomUserId', 10, 1 );
    
    function showCustomUserId() {
    	$output = '';
    
    	if ( is_user_logged_in() ) {
    		$user_id   = get_current_user_id();
    		$custom_id = get_user_meta( $user_id, 'custom_user_id', true );
    
    		$output .= '<div class="custom_id_div">';
    		$output .= '<p>';
    		$output .= $custom_id;
    		$output .= '</p>';
    		$output .= '</div>';
    
    		return $output;
    	}
    }
    
    add_shortcode( 'custom_user_id', 'showCustomUserId' );
    
    

    All I have done is added 1 line

    
    $batch_size    = 10000;
    

    and updated your batch number calculation to this

    
    $batch_number  = 1 + intval( ( $user_id - 1 ) / $batch_size );
    
    

    That will add the correct batch number.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved