How To Generate Unique User Code for Users after Registration
1. First off, we need a unique code generator function. Copy this code and paste it on your active theme’s functions.php file:
function ali_generate_unique_user_code( $prefix ){ global $wpdb; $randomize = rand(11111,99999); $appex = $randomize; $user_code = $prefix . $appex; $sql = "SELECT COUNT(*) FROM $wpdb->usermeta WHERE `meta_key` LIKE '_ali_unique_user_code' AND `meta_value` LIKE %s"; $is_exist = $wpdb->get_var( $wpdb->prepare( $sql, $user_code ) ); if( !$is_exist ){ return $user_code; } return ali_generate_unique_user_code( $prefix ); }
This function takes one argument which is the $prefix. It generates a random number between 11111 and 99999, concatenates the $prefix and the generated number which will be the final user unique code but before that, this function will also check if the generated code already exists in the database. This is to avoid duplications since we’re only generating a random number and we all know that it might generate the same number again. If there is no existing code within the database, it will return the unique code. Otherwise, it will generate a new one.
2. Here is the hook to generate the user code after registration:
function ali_add_unique_user_code( $user_id, $user_data ){ $country = trim( $user_data['billing_country'] ); $prefix = ali_country_list()[$country]; $unique_code = ali_generate_unique_user_code( $prefix ); update_user_meta( $user_id, '_ali_unique_user_code', $unique_code ); } add_action( 'wpcfe_after_user_registration_success', 'ali_add_unique_user_code', 10, 2 );
As you have noticed, there is a $country variable. This is a simple modification feature that will change the prefix depending on the country selected by the user on registration. You will also notice that there is an ali_country_list() which will be discussed below:
3. Here is the country list:
function ali_country_list() { $countries = array( 'Trinidad and Tobago' => 'TT', 'Jamaica' => 'CS', 'United States (US)' => 'CM' ); return apply_filters( 'ali_country_list', $countries ); }
As you can see, this function returns an array of country names as keys and prefixes as values. If you go back to number 2, you will see that we used this function and what we did there was simply used the country selected by the user as key to return the desired value which is the prefix.
This:
$country = trim( $user_data['billing_country'] ); $prefix = ali_country_list()[$country];
Is equivalent to:
$country = trim( $user_data['billing_country'] ); $prefix = $countries['Jamaica'];
If you want to add new key-value pairs (i.e “Philippines” => “PH” ), use this filter which is present above:
function add_new_country_prefix_callback( $countries ) { $countries['Philippines'] = 'PH'; return $countries; } add_filter( 'ali_country_list', 'add_new_country_prefix_callback' );
4. Next is we’re going to display the unique code on users list ( this requires WPCargo User Management Addon ):
function wpcumanage_user_table_header_unique_code(){ echo "<th>".__('Unique ID', 'wpcargo-customizer')."</th>"; } add_action( 'wpcumanage_user_table_header', 'wpcumanage_user_table_header_unique_code' ); function wpcumanage_user_table_data_unique_code( $user ){ echo '<td>'.get_user_meta( (int)$user->ID, '_ali_unique_user_code', true ).'</td>'; } add_action( 'wpcumanage_user_table_data', 'wpcumanage_user_table_data_unique_code' );
5. If ever there are existing clients that doesn’t have a unique code yet, you can use this code:
function ali_wpmuc_add_unique_code( $newUser ){ $_user_id = (int)$newUser->ID; $country = trim( get_user_meta( $_user_id, 'billing_country', true ) ); $prefix = ali_country_list()[$country]; if( !get_user_meta( $_user_id, '_ali_unique_user_code', true ) ){ update_user_meta( $_user_id, '_ali_unique_user_code', ali_generate_unique_user_code( $prefix ) ); } } add_action( 'wpcumanage_after_save_user', 'ali_wpmuc_add_unique_code', 10, 1 );
6. If you want to display the user’s unique code on shipments table, copy this code:
function ali_wpcfe_shipment_table_header_callback(){ echo "<th>".__('Unique ID', 'wpcargo-customizer')."</th>"; } add_action('wpcfe_shipment_table_header', 'ali_wpcfe_shipment_table_header_callback'); function ali_wpcfe_shipment_table_data_callback( $shipment_id ){ $_registered_shipper = get_post_meta( $shipment_id, 'registered_shipper', true ); $_user_unique_code = get_user_meta( $_registered_shipper, '_ali_unique_user_code', true ); echo "<td>".$_user_unique_code."</td>"; } add_action("wpcfe_shipment_table_data", "ali_wpcfe_shipment_table_data_callback");