Automatically generate username number – Instead of default Username
-
How can I automatically generate username number, instead of leaving the choice to the user to take any username ?
First user would be #1000, second #1001, 1003, etc…
I am working on a project where each user have an ID number. I would like to use the default WordPress username field for this ID number.
So, the user use is ID number + password to connect to the site.
** I will also install a ‘login with e-mail’ plugin so user will be easily abble to connect if he do not remember is number, perfect for brand new member **
-
Here’s my half baked ideas on this, I don’t know how doable these are, such as causing an unforeseen errors, etc.
In order to get a user ID, you must first insert a user, which means you must provide a login name. But it could always be “TBD” or something since it will be soon changed anyway. It would only be an issue if two processes try to add a user at the same time. Maybe you could pre-populate the field somehow with javascript, and possibly even hide it with CSS. But there must be a field, and it must have a value that is not in the DB.
Then perhaps hook the action ‘user_register’, get the just inserted user data. Unset the user_pass field to indicate no password change. Place the user ID in the user login, nicename, display_name fields and call wp_update_user() to change the names in the DB.
Completely untested, but I think worth a try, good luck.
Actually, I would like the user login name to be the number. So the user connect with is ‘id’ ( which is for WordPress is regular Login ) .
The only difference is than the user didn’t chose it himself…
Thanks for sharing your ideas
Actually, my suggestion will do exactly that 🙂
“Place the user ID in the user login (field)” User ID is the one assigned by mySQL during initial user insertion.
I think you had in mind a different approach entirely. If it involves checking the login form field directly against the DB user_ID field, it would be difficult to implement cleanly.
<?php /* Plugin Name: Auto Generated numbered Username Version: 1.0 Plugin URI: Description: Simple WordPress plugin to automatically generate numbered username, hook in to wordpress user registration page and automaitcally add numbered Username in "Username" Field. */ // global variable next username // @var $next_username global $next_username; // global default variable starting username number // @var $starting_username global $starting_username; // Start from numbered username from 1001 if previous numbered username is not found in users table, // you can set this value to any number before adding any numbered username in database; $starting_username = 1001; // current page global $pagenow; // add jquery in head section of registration page if( $pagenow == 'wp-login.php' && $_REQUEST['action'] == 'register') add_action('init', 'add_scripts_method' ); // hook in to user registration form add_action('register_form', 'add_default_username'); // add in theme header add_action('wp_head', 'add_scripts_method' ); add_action('wp_head', 'add_default_username'); // add action hook to redirect new user add to edit profile page add_action("admin_init", "redirect_user_add"); // check if which page we currently are in if( $pagenow == 'user-new.php') // add action to admin footer add_action('admin_footer', 'show_next_available_username' ); // Function to add numbered username in user registration page function add_default_username() { // get next username $next_username = get_next_available_username(); // add $next_username to user login field value in user registration form // and this field to redonly so that user cannot changed using jQuery ?> <script type="text/javascript"> jQuery(document).ready( function(){ var username = '<?php echo $next_username; ?>'; var message = '<p class="message register" style="padding:5px; margin:0 0 10px 0">Username is auto-generated and can not be changed.</p>'; // message jQuery('input[name="user_login"]').val( username ); jQuery('input[name="user_login"]').attr('readonly', 'readonly'); jQuery('label[for="user_login"]').after( message ); /*** to work with wp profile builder plugin **/ var message1 = '<label class="note" style="padding: 5px; margin: 0px 0px 10px; float: right; width: 65%; color: red;">Username is auto-generated and can not be changed.</label>'; // message jQuery('#wppb_register input[name="user_name"]').val( username ); jQuery('#wppb_register input[name="user_name"]').attr('readonly', 'readonly'); jQuery('#wppb_register input[name="user_name"]').after( message1 ); } ); </script> <?php } // Display next available username in add new user page function show_next_available_username() { // add jquery add_action('wp_enqueue_scripts', 'my_scripts_method'); // get next username $next_username = get_next_available_username(); // display next available username using jqeury ?> <script type="text/javascript"> jQuery(document).ready( function(){ var username = '<?php echo $next_username; ?>'; var message = '<p class="update-nag" style="width:24em; padding:5px; margin:5px 0">Next available Username #<strong>'+username+'</strong></p>'; // message jQuery('input[name="user_login"]').after( message ); // add onsubmit attribute to form jQuery('form#createuser').attr('onsubmit', 'return checkUsername()'); } ); // auto add username if username field is blank function checkUsername() { var username = jQuery('input[name="user_login"]').val(); if( username == '') { var username = '<?php echo $next_username; ?>'; jQuery('input[name="user_login"]').val(username); } return true; } </script> <?php } // function to get next available username function get_next_available_username() { // global DB variable/object global $wpdb; global $starting_username; // get all username/number from database $query = 'SELECT user_login FROM '.$wpdb->users; $usernames = $wpdb->get_col( $query ); if( $usernames ) { $i = 0; // starting array index $numeric_usernames = array(); foreach( $usernames as $username ) { if( is_numeric( $username ) ) { $numeric_usernames[ $i ] = $username; $i++; } } // check if numeric usernames are empty if( !empty( $numeric_usernames ) ) { // get the latest/maximum value of usernames $last_username = max( $numeric_usernames ); $next_username = $last_username + 1; return $next_username; } else { return $starting_username; } } else { return $starting_username; } } // redirect new user add to edit profile page function redirect_user_add() { if(!empty($_GET['update']) && $_GET['update'] == 'add' && !empty($_GET['id']) ) { $id = $_GET['id']; //$user = get_userdatabylogin($username); wp_redirect( admin_url("/user-edit.php?user_id=".$id ) ); } } // enqueue jquery library function add_scripts_method() { wp_deregister_script( 'jquery' ); wp_enqueue_script( 'jquery', plugins_url('/jquery-1.7.1.min.js', __FILE__), array(), '1.7.1'); wp_enqueue_script( 'jquery' ); } ?>
Do not forget jquery-1.7.1.min.js and it is working like a charm !
Hi guillaumes!
Thanks for a brilliant script!
I am facing a bit of a problem since if a user forgot his/her password, then a generated Number username automatically is added to the form.Is there a way to correct this?
Many thanks!
EugUK
AWESOME!!!
Be great to offer this as a plugin in the plugin repository.
- The topic ‘Automatically generate username number – Instead of default Username’ is closed to new replies.