Title: Auto username problem
Last modified: August 22, 2016

---

# Auto username problem

 *  [JabaMS](https://wordpress.org/support/users/jabams/)
 * (@jabams)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/auto-username-problem/)
 * Hi, i’m using this plugin to auto generate username on registration:
 *     ```
       <?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' );
       	}
       ?>
       ```
   
 * 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?
    By the way, I’m usig classipress theme if that
   is important. I need auto username because of author pages, i don’t want usernames
   in url’s, number’s are better 😀

Viewing 1 replies (of 1 total)

 *  Thread Starter [JabaMS](https://wordpress.org/support/users/jabams/)
 * (@jabams)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/auto-username-problem/#post-5801932)
 * No one?

Viewing 1 replies (of 1 total)

The topic ‘Auto username problem’ is closed to new replies.

 * 1 reply
 * 1 participant
 * Last reply from: [JabaMS](https://wordpress.org/support/users/jabams/)
 * Last activity: [11 years, 4 months ago](https://wordpress.org/support/topic/auto-username-problem/#post-5801932)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
