I'm noticing a weird issue on a custom User Registration page I'm using in WordPress. It seems as if WordPress will only accept usernames with 16 characters (NO MORE, NO LESS).
The custom registration form asks for First Name, Last Name, Email Address and a Number. To prevent duplicate Usernames, the username is generated by adding First and Last Name (set to lowercase) and then add 9 characters (0-9 and a-z) randomly generated at the end.
Here is the function to generate the 9 random characters:
function generateRandomString($length = 9) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
Here is the code to make the username:
$user_login = strtolower( $fname ) . strtolower( $lname ) . generateRandomString();
This function was working a few days ago and now stopped working. I'm trying to figure out what changed over the weekend but haven't been able to find out.
Any ideas why the character limitation is there?
UPDATE 1
Here is the code I use to register new users. It creates a form and uses wp_insert_user
:
function ppt_add_user() {
global $wp_error; ?>
<?php if ( current_user_can( 'edit_church_info' ) ) : ?>
<h4><?php _e( 'Add New Member', 'ppt-church' ); ?></h4>
<?php
if ( isset( $_POST['ppt_new_user_submit'] ) ) {
$errors = array();
$fname = sanitize_text_field( $_POST['fname'] );
$lname = sanitize_text_field( $_POST['lname'] );
$user_login = strtolower( $fname ) . strtolower( $lname ) . generateRandomString();
$email = trim( $_POST['user_email'] );
$error = null;
$error = ppt_register_new_user( $user_login, $email, $fname, $lname );
if ( !is_wp_error( $error ) ) {
echo '<div class="alert alert-success">' . __( 'User Added', 'ppt-church' ) . '</div>';
} else {
echo '<div class="alert alert-danger">' . $error->get_error_message() . '</div>';
}
} ?>
<form action="" method="post">
<div class="ppt-post-form">
<div class="form-group">
<label for="fname">First Name</label>
<input type="text" class="form-control" name="fname" value="<?php if( isset( $_POST['fname']) ? $fname : null ); ?>">
</div>
<div class="form-group">
<label for="lname">Last Name</label>
<input type="text" class="form-control" name="lname" value="<?php if( isset( $_POST['lname']) ? $lname : null ); ?>">
</div>
<div class="form-group">
<label for="user_email">Email</label>
<input type="email" class="form-control" name="user_email" value="<?php if( isset( $_POST['user_email']) ? $email : null ); ?>">
</div>
<div class="form-group">
<label for="mday">Day of the month</label>
<input type="number" class="form-control" name="mday" value="<?php if( isset( $_POST['mday']) ? $mday : null ); ?>" min="1" max="31">
</div>
<button type="submit" class="btn btn-primary" name="ppt_new_user_submit">Add Member</button>
</div>
</form>
<br><br>
<div class="panel panel-success">
<div class="panel-heading">Useful Information</div>
<div class="panel-body">
<p>Thank you for investing the time to set up a team of men to pray for and affirm your pastor. His life and the life of your church will never be the same as a result of your initiative.</p>
<p>Here are a some tips:</p>
<ol>
<li>Don’t wait until you have a full roster to enter names. Get two or three men in addition to yourself and then share the url of your church page with men via email so they can see what it will look like for your pastor to have a Pastor Prayer Team</li>
<li>Each date of the month will need a unique email address.</li>
<li>If you are setting up pages for more than one pastor and a man wants to pray for additional pastors then he will need additional email addresses.</li>
<li>As you begin to fill your monthly calendar it is then a good time to ask a man if he will pray a certain date of the month.</li>
<li>Broadcast emails to the men of your church typically will not be very effective but may be enough to solicit a few men and get you started. Filling your roster will require giving men a personal invitation to join you.</li>
</ol>
</div>
</div>
<?php endif; ?>
<?php
}
function ppt_register_new_user( $user_login, $user_email, $fname, $lname ) {
$errors = new WP_Error();
// $user_login = strtolower( $fname ) . '_' . strtolower( $lname ) . '_' . generateRandomString();
$sanitized_user_login = sanitize_user( $user_login );
//$user_email = apply_filters( 'user_registration_email', $user_email );
// Check the username
if ( $sanitized_user_login == '' ) {
$errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.', 'ppt-church' ) );
} elseif ( !validate_username( $user_login ) ) {
$errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', 'ppt-church' ) );
$sanitized_user_login = '';
} elseif ( username_exists( $sanitized_user_login ) ) {
$errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.', 'ppt-church' ) );
}
// Check the e-mail address
if ( $user_email == '' ) {
$errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.', 'ppt-church' ) );
} elseif ( !is_email( $user_email ) ) {
$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.', 'ppt-church' ) );
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
$errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.', 'ppt-church' ) );
}
$mday = $_POST['mday'];
$days = array();
$user_args = array(
'meta_key' => 'prayer_day'
);
$user_query = new WP_User_Query( $user_args );
foreach ( $user_query->results as $user ) {
$days[] = $user->prayer_day;
}
//
// //$user_day = get_user_meta( $profileuser->ID, 'prayer_day', true );
//
if( in_array( $mday, $days ) ) {
$errors->add( 'day_exists', __( '<strong>ERROR</strong>: That day has already been taken.', 'ppt-church' ) );
}
do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
if ( $errors->get_error_code() )
return $errors;
$user_pass = wp_generate_password( 12, false );
//$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
$userdata = array(
'user_login' => $sanitized_user_login,
'user_email' => $user_email,
'user_pass' => $user_pass,
'first_name' => $fname,
'last_name' => $lname,
'role' => 'team_member'
);
$user_id = wp_insert_user( $userdata );
if ( !$user_id ) {
$errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:%s">webmaster</a> !', 'ppt-church' ), get_option( 'admin_email' ) ) );
return $errors;
}
update_user_meta( $user_id, 'prayer_day', $mday );
update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.
// wp_new_user_notification( $user_id, $user_pass );
return $user_id;
}
UPDATE 2
The form seems to work if the sum of the characters for First and Last name equal 7 (the total would be 16 with the 9 randomly generated characters added) or it will also work if the First Name has 8 characters. This has me confused.