WordPress函数register_new_user()注册新用户
在 WordPress CMS内容管理系统中,register_new_user()函数是 WordPress 中内置函数。WordPress PHP 函数register_new_user()注册新用户
WordPress 中的该register_new_user
函数用于处理新用户帐户的注册。当调用此函数时,它会执行一系列操作以确保新用户在 WordPress 系统中正确注册。该函数首先验证提供的用户数据,例如用户名和电子邮件地址。它会检查用户名的唯一性,并验证电子邮件地址是否尚未与现有帐户关联。如果数据通过这些检查,该函数将继续在 WordPress 数据库中创建新的用户记录。
创建用户记录后,该register_new_user
功能会为新用户分配默认角色和功能。这可确保新用户根据预定义的角色具有与 WordPress 网站交互的适当权限。此外,该函数还会触发一系列操作和钩子,让开发人员能够扩展或修改注册流程。这些钩子可用于发送欢迎电子邮件、记录注册事件或与第三方服务集成。总之,该register_new_user
函数自动执行在 WordPress 中创建和配置新用户帐户的过程,处理验证、数据库插入和角色分配,同时通过钩子提供可扩展性。
推荐:[最新版]WordPress SEO插件Rank Math Pro
register_new_user()函数基本语法
描述
检查当前帖子是否具有任何给定的标签
用法
$user_login = "johndoe";
$user_email = "john.doe@example.com";
$user = register_new_user($user_login, $user_email);
$user_login
string:必填,用户登录的用户名。$user_email
字符串:必填。用于发送密码和添加的用户电子邮件地址。
推荐:Swift Performance插件教程WordPress缓存性能优化插件
register_new_user()函数
在 WordPress 中,该has_tag()
函数register_new_user 函数将新用户插入 WordPress 数据库。当新用户通过 WordPress 的登录页面注册时,将使用此函数。它与wp_create_user()不同之处在于,它需要有效的用户名和电子邮件地址,但不允许选择密码,而是使用
wp_generate_password()生成随机密码。本身不处理用户创建,它仅检查提交的用户名和电子邮件的有效性并生成随机密码(源文件可参考这里)
function register_new_user( $user_login, $user_email ) {
$errors = new WP_Error();
$sanitized_user_login = sanitize_user( $user_login );
/**
* Filters the email address of a user being registered.
*
* @since 2.1.0
*
* @param string $user_email The email address of the new user.
*/
$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.' ) );
} 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.' ) );
$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.' ) );
} else {
/** This filter is documented in wp-includes/user.php */
$illegal_user_logins = (array) apply_filters( 'illegal_user_logins', array() );
if ( in_array( strtolower( $sanitized_user_login ), array_map( 'strtolower', $illegal_user_logins ), true ) ) {
$errors->add( 'invalid_username', __( '<strong>Error:</strong> Sorry, that username is not allowed.' ) );
}
}
// Check the email address.
if ( '' === $user_email ) {
$errors->add( 'empty_email', __( '<strong>Error:</strong> Please type your email address.' ) );
} elseif ( ! is_email( $user_email ) ) {
$errors->add( 'invalid_email', __( '<strong>Error:</strong> The email address is not correct.' ) );
$user_email = '';
} elseif ( email_exists( $user_email ) ) {
$errors->add(
'email_exists',
sprintf(
/* translators: %s: Link to the login page. */
__( '<strong>Error:</strong> This email address is already registered. <a href="%s">Log in</a> with this address or choose another one.' ),
wp_login_url()
)
);
}
/**
* Fires when submitting registration form data, before the user is created.
*
* @since 2.1.0
*
* @param string $sanitized_user_login The submitted username after being sanitized.
* @param string $user_email The submitted email.
* @param WP_Error $errors Contains any errors with submitted username and email,
* e.g., an empty field, an invalid username or email,
* or an existing username or email.
*/
do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
/**
* 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 be 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.
*/
$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
if ( $errors->has_errors() ) {
return $errors;
}
$user_pass = wp_generate_password( 12, false );
$user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
if ( ! $user_id || is_wp_error( $user_id ) ) {
$errors->add(
'registerfail',
sprintf(
/* translators: %s: Admin email address. */
__( '<strong>Error:</strong> Could not register you… please contact the <a href="mailto:%s">site admin</a>!' ),
get_option( 'admin_email' )
)
);
return $errors;
}
update_user_meta( $user_id, 'default_password_nag', true ); // Set up the password change nag.
if ( ! empty( $_COOKIE['wp_lang'] ) ) {
$wp_lang = sanitize_text_field( $_COOKIE['wp_lang'] );
if ( in_array( $wp_lang, get_available_languages(), true ) ) {
update_user_meta( $user_id, 'locale', $wp_lang ); // Set user locale if defined on registration.
}
}
/**
* Fires after a new user registration has been recorded.
*
* @since 4.4.0
*
* @param int $user_id ID of the newly registered user.
*/
do_action( 'register_new_user', $user_id );
return $user_id;
}
推荐:Max Mega Menu插件教程WordPress添加超级菜单
如何使用register_new_user()
基本注册使用给定的用户名和电子邮件地址注册新用户。
$user_login = "johndoe";
$user_email = " john.doe@example.com ";
$user = register_new_user($user_login, $user_email);
重定向注册注册新用户并在注册成功后将其重定向到自定义页面。
$user_login = "janedoe";
$user_email = " jane.doe@example.com ";
$user = register_new_user($user_login, $user_email);
if (!is_wp_error($user)) {
$redirect_to = "welcome.php";
wp_safe_redirect($redirect_to);
exit();
}
带有错误处理的注册注册新用户,如果注册失败则显示错误消息。
$user_login = "johndoe";
$user_email = " john.doe@example.com ";
$user = register_new_user($user_login, $user_email);
if (is_wp_error($user)) {
echo $user->get_error_message();
} else {
echo "注册成功!";
}
使用自定义用户角色注册使用自定义用户角色注册新用户。
$user_login = "johndoe";
$user_email = " john.doe@example.com ";
$user_id = register_new_user($user_login, $user_email);
if (!is_wp_error($user_id)) {
$user = new WP_User($user_id);
$user->set_role('custom_role');
}
使用附加用户元数据进行注册注册新用户并添加额外的用户元数据。
$user_login = "johndoe";
$user_email = " john.doe@example.com ";
$user_id = register_new_user($user_login, $user_email);
if (!is_wp_error($user_id)) {
add_user_meta($user_id, 'phone_number', '123-456-7890');
}
如何注册新用户
function register_user_with_error_handling($username, $email) {
$user_id = register_new_user($username, $email);
if (is_wp_error($user_id)) {
$error_message = $user_id->get_error_message();
// Handle the error accordingly
echo 'Registration failed: ' . $error_message;
} else {
echo 'User registered successfully with ID: ' . $user_id;
}
}
register_user_with_error_handling('sampleuser', 'sampleuser@example.com');
如何循环注册多个用户
function register_multiple_users($users) {
foreach ($users as $user) {
$user_id = register_new_user($user['username'], $user['email']);
if (is_wp_error($user_id)) {
echo 'Error registering ' . $user['username'] . ': ' . $user_id->get_error_message() . '<br>';
} else {
echo 'User ' . $user['username'] . ' registered successfully with ID: ' . $user_id . '<br>';
}
}
}
$users_to_register = [
['username' => 'user1', 'email' => 'user1@example.com'],
['username' => 'user2', 'email' => 'user2@example.com'],
];
register_multiple_users($users_to_register);
Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折