这完全取决于用户在您的网站上注册的方式。
TLDR 下面的代码允许您从指定自定义用户名的管理区域创建自定义用户,或使用GENERATE_CUSTOM_SEQ_USERNAME 生成顺序编号的。如果使用注册表,而不是wp-login.php 刚刚设置好user_login 到GENERATE_CUSTOM_SEQ_USERNAME 并使用CSS隐藏字段。
如果他们将通过wp-login.php 注册页面,首先需要允许为空user_login 输入字段:
/**
* Filters the errors encountered when a new user is being registered.
*
* The filtered WP_Error object may, for example, contain errors for an invalid
* or existing username or email address. A WP_Error object should always returned,
* but may or may not contain errors.
*
* If any errors are present in $errors, this will abort the user\'s registration.
*
* @since 2.1.0
*
* @param WP_Error $errors A WP_Error object containing any errors encountered
* during registration.
* @param string $sanitized_user_login User\'s username after it has been sanitized.
* @param string $user_email User\'s email.
*/
add_filter( \'registration_errors\', \'smyles_allow_wp_login_register_empty_user_login\', 9999, 3 );
/**
* Allow empty user_login (username) from wp-login.php registration form
*
*
* @since @@version
*
* @param $errors WP_Error
* @param $sanitized_user_login
* @param $user_email
*
* @return mixed
*/
function smyles_allow_wp_login_register_empty_user_login( $errors, $sanitized_user_login, $user_email ){
// First remove empty_username error code to make sure there aren\'t any other errors
$errors->remove( \'empty_username\' );
$error_codes = $errors->get_error_codes();
// Return errors and don\'t process further (we only want to proceed when empty_username is only error code)
if( ! empty( $error_codes ) ){
return $errors;
}
return $errors;
}
这是通过过滤注册错误来完成的
wp-login.php 以及移除
empty_username 一
接下来,您将需要一个helper函数来生成带有填充零的顺序用户名。此函数使用10个字符的长度,每个新用户增加一个字符。这还验证了在将user\\u登录设置为该值之前,该登录不存在。
/**
* Generate sequential padded user_login
*
* @author Myles McNamara
*
* @return string
*/
function smyles_generate_next_seq_user_login(){
// Use 0 as default (if option does not exist yet), as 1 will be used for first user
$last_user_num = get_option( \'smyles_custom_seq_usernames_last_id\', 0 );
// Create padded username total length of 10 characters, increasing last ID by 1
$gen_user_login = str_pad( (int) $last_user_num + 1, 10, 0, STR_PAD_LEFT );
if( username_exists( $gen_user_login ) ){
// If generated new user login exists, update our last id +1 and do recursive call
update_option( \'smyles_custom_seq_usernames_last_id\', (int) $last_user_num + 1 );
return smyles_generate_next_seq_user_login();
}
return $gen_user_login;
}
然后需要在
user_login 由处理
wp_insert_user 作用因为我们允许
wp-login.php 传递空
user_login 从
register_new_user 函数,然后可以检查该值是否为空,并返回自定义生成的值。
你也会注意到我加入了GENERATE_CUSTOM_SEQ_USERNAME 检查是否通过user_login, 如果要使用CSS隐藏用户登录字段(在任何其他注册表中),并让其生成user_login 不会为空用户名引发错误。由于该用户名在技术上永远不应该存在,您可以将其用作占位符来确定是否应生成用户名。
This also allows you to create a new user in admin area, and just set GENERATE_CUSTOM_SEQ_USERNAME as username, and it will generate sequential numbered one for you, 或者创建自己的自定义。
add_filter( \'pre_user_login\', \'smyles_custom_username_seq_pre_user_login\' );
/**
* Set user_login to generated value, only if passed value is empty
*
* @author Myles McNamara
*
* @param $sanitized_user_login
*
* @return string
*/
function smyles_custom_username_seq_pre_user_login( $sanitized_user_login ){
$sanitized_user_login = trim( $sanitized_user_login ); // to match wp_insert_user handling
/**
* The user_login should be empty string when creating from wp-login.php registration page,
* otherwise will contain a value when called by something else.
*
* There is a chance this will be called for updating a user (which is incorrect as wp_update_user should be called),
* but even when updating a user, the passed user_login should have some type of value.
*/
if( empty( $sanitized_user_login ) || $sanitized_user_login === \'GENERATE_CUSTOM_SEQ_USERNAME\' ){
$sanitized_user_login = smyles_generate_next_seq_user_login();
}
return $sanitized_user_login;
}
下一步是更新选项中最后使用的ID,但只有在验证用户确实已创建(并且没有其他错误导致创建用户失败)之后:
/**
* Filters a user\'s meta values and keys immediately after the user is created or updated
* and before any user meta is inserted or updated.
*
* Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
*
* @since 4.4.0
*
* @param array $meta {
* Default meta values and keys for the user.
*
* @type string $nickname The user\'s nickname. Default is the user\'s username.
* @type string $first_name The user\'s first name.
* @type string $last_name The user\'s last name.
* @type string $description The user\'s description.
* @type bool $rich_editing Whether to enable the rich-editor for the user. False if not empty.
* @type bool $syntax_highlighting Whether to enable the rich code editor for the user. False if not empty.
* @type bool $comment_shortcuts Whether to enable keyboard shortcuts for the user. Default false.
* @type string $admin_color The color scheme for a user\'s admin screen. Default \'fresh\'.
* @type int|bool $use_ssl Whether to force SSL on the user\'s admin area. 0|false if SSL is
* not forced.
* @type bool $show_admin_bar_front Whether to show the admin bar on the front end for the user.
* Default true.
* }
*
* @param WP_User $user User object.
* @param bool $update Whether the user is being updated rather than created.
*/
add_filter( \'insert_user_meta\', \'smyles_custom_username_seq_verify\', 10, 3 );
/**
* Verify user was created, and then increase option value
*
*
* @author Myles McNamara
*
* @param $meta
* @param $user WP_User
* @param $update
*
* @return mixed
*/
function smyles_custom_username_seq_verify( $meta, $user, $update ){
// Don\'t want to verify if this is just an update call
if( ! $update && $user && $user->user_login ){
// Check what the last user num was stored as
$last_user_num = get_option( \'smyles_custom_seq_usernames_last_id\', 0 );
// Verify that user_login of the user that was created, matches what is supposed to be the next user_login
if( (int) $last_user_num + 1 === (int) $user->user_login ){
// Update our option after verification
update_option( \'smyles_custom_seq_usernames_last_id\', (int) $user->user_login ); // Type casting to int causes 0000000001 to be 1 (trim leading zeros)
}
}
return $meta;
}
然后,您可能希望隐藏wp登录上的userlogin输入。php表单,您可以使用一些简单的CSS:
add_action( \'register_form\', \'smyles_hide_wp_login_username_field\' );
function smyles_hide_wp_login_username_field(){
// Hide the first <p> element in registerform, which is the userlogin input
echo \'<style>#registerform p:first-of-type { display: none; }</style>\';
}
https://gist.github.com/tripflex/1acacc19467122cdb1171a5e0b7300dd