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&#8217;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&#8217;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.

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).

You can check the database description and you should see that the user_login column is varchar(60)... much, much greater than 16. I am not sure if there is a minimum length but login names much shorter than 16 are allowed.

Whatever is causing this problem isn't WordPress Core.

  • I am aware of the 60 Character limit in the DB and also aware that it's not WP Core. I guess I asked the final question incorrectly. Why does my script seem to limit usernames to only 16 characters? – katart Dec 31 '15 at 17:12
  • The code you posted doesn't limit the string to 16. That is very easy to test, just pass $fname and $lname as very long strings. You will get > 16. The problem is elsewhere. – s_ha_dum Dec 31 '15 at 17:18
  • If I use a $fname with 3 characters and a $lname with 4 characters, the user is registered since the character count == 16. Any other combination that is > or < will not register a new user. Do you have an idea where the problem might be? Would you want to see the registration function? – katart Dec 31 '15 at 17:40
  • Like I said, the code you posted does not limit anything. The problem is elsewhere. Yes, if you have more code post that-- preferably enough that the issue can be replicated. – s_ha_dum Dec 31 '15 at 17:49
  • I updated the original post – katart Dec 31 '15 at 17:56

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.