我在WordPress中使用的自定义用户注册页面上发现了一个奇怪的问题。WordPress似乎只接受16个字符的用户名(不多也不少)。
自定义注册表要求输入名字、姓氏、电子邮件地址和号码。为了防止重复用户名,通过添加名字和姓氏(设置为小写)生成用户名,然后在末尾添加随机生成的9个字符(0-9和a-z)。
以下是生成9个随机字符的函数:
function generateRandomString($length = 9) {
$characters = \'0123456789abcdefghijklmnopqrstuvwxyz\';
$charactersLength = strlen($characters);
$randomString = \'\';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
以下是生成用户名的代码:
$user_login = strtolower( $fname ) . strtolower( $lname ) . generateRandomString();
该函数几天前还在工作,现在已停止工作。我想知道周末发生了什么变化,但一直没有发现。
你知道为什么会有性格限制吗?
UPDATE 1
这是我用来注册新用户的代码。它创建一个表单并使用
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
如果名字和姓氏的字符之和等于7(加上9个随机生成的字符,总数将为16),则该表单似乎有效;如果名字有8个字符,则该表单也有效。这让我感到困惑。