WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/register new user

Contents

Description

The register_new_user() function inserts a new user into the WordPress database. This function is used when a new user registers through WordPress' Login Page. It differs from wp_create_user() in that it requires a valid username and email address but doesn't allow to chose a password, generating a random one using wp_generate_password(). If you want to create a new user with a specific password or with additional parameters, use wp_create_user() or wp_insert_user() instead.

register_new_user() doesn't handle the user creation itself, it simply checks the submitted username and email validity and generates a random password, relying on wp_create_user() to create the new User. If registration worked it sends a notification email to the user with his password using wp_new_user_notification(). In case of registration failure it returns a WP_Error().

register_new_user() also provides two useful hooks to customize validation rules or user registration process, register_post and registration_errors.

Usage

 <?php register_new_user$user_login$user_email ); ?> 

Example

As used in wp-login.php:

$user_login = $_POST['user_login'];
$user_email = $_POST['user_email'];
$errors = register_new_user($user_login, $user_email);
if ( !is_wp_error($errors) ) {
	$redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
	wp_safe_redirect( $redirect_to );
	exit();
}

Parameters

$user_login
(string) (required) User's username for logging in
Default: None
$user_email
(string) (required) User's email address to send password and add
Default: None

Returns

Like wp_create_user(), when successful this function returns the user ID of the created user. In case of failure (login or email is empty, invalid, already existing, or wp_create_user() failed) the function returns an error object.

Source File

register_new_user() is located in wp-includes/user.php.

See also index of Function Reference and index of Template Tags.