我们收到了来自客户的许多请求,询问是否可以创建一个 WordPress 注册表单,让用户无需用户名即可注册。 从技术上讲,这是不可能的,因为 WordPress 要求所有用户都有一个用户名。
在本教程中,我将向我们展示如何创建一个没有用户名字段的用户注册表单的万无一失的方法。
建立注册表
* 打开编辑界面 简介新闻登记表 您正在通过在 WordPress 后端单击其标题来使用。
* 删除用户名块或短代码(如果您使用的是 高级表单生成器)。
* 转到表单设置并检查 禁用用户名要求 复选框。

完成上述步骤后,通过注册表进行注册的用户将自动从他们的电子邮件地址为他们生成一个用户名。
例如,假设用户注册了 johndoe@gmail.com
, johndoe
自动成为他的用户名。 如果用户名已经存在,它就会变成“johndoe1”。

修改注册错误
注意:这篇文章中的所有代码片段都应该进入你的主题 functions.php
文件或 特定于站点的插件.
以下是用户名生成的错误消息 WordPress 注册表单.
- 用户名不得超过 60 个字符。
- 抱歉,该用户名已存在!
- 抱歉,不允许使用该用户名。
下面的代码将这些错误消息中的“用户名”替换为“电子邮件地址”。
add_filter( 'gettext', 'change_registration_username_errors', 10, 3 );
function change_registration_username_errors( $translations, $text, $domain ) {
if ( $domain == 'default' ) {
if ( $text == 'Username may not be longer than 60 characters.' ) {
$translations="Email address may not be longer than 60 characters.";
} elseif ( $text == 'Sorry, that username already exists!' ) {
$translations="Sorry, that email address already exists!";
} elseif ( $text == 'Sorry, that username is not allowed.' ) {
$translations="Sorry, that email address is not allowed.";
}
}
return $translations;
}
修改登录错误
我们现在应该完成了,但我们仍然需要修改用户名中产生的错误 WordPress登录表单 使其万无一失。
下面的代码做得非常好。
add_filter( 'gettext', 'change_login_username_errors', 10, 3 );function change_login_username_errors( $translations, $text, $domain ) {
if ( $domain == 'default' ) {
if ( $text == '<strong>ERROR</strong>: Invalid username.' ) {
$translations="<strong>ERROR</strong>: Invalid email.";
} elseif ( $text == '<strong>ERROR</strong>: The password you entered for the username %s is incorrect.' ) {
$translations="<strong>ERROR</strong>: The password you entered for the email address %s is incorrect.";
} elseif ( $text == '<strong>ERROR</strong>: The username field is empty.' ) {
$translations="<strong>ERROR</strong>: The email field is empty.";
} elseif ( $text == '<strong>ERROR</strong>: Invalid username or incorrect password.' ) {
$translations="<strong>ERROR</strong>: Invalid email address or incorrect password.";
}
}
return $translations;
}
结论
我们希望我们已经能够教您如何构建一个 WordPress(自定义)注册表单,用户可以在其中注册而无需询问他们的用户名。