Title: register_new_user
Published: April 25, 2014
Last modified: August 20, 2026

---

# register_new_user( string $user_login, string $user_email ): int|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#user-contributed-notes)

Handles registering a new user.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#parameters)󠁿

 `$user_login`stringrequired

User’s username for logging in

`$user_email`stringrequired

User’s email address to send password and add

## 󠀁[Return](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#return)󠁿

 int|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/) Either
user’s ID or error on failure.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#more-information)󠁿

The **[register_new_user()](https://developer.wordpress.org/reference/functions/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()](https://codex.wordpress.org/Function_Reference/wp_create_user)
in that it requires a valid username and email address but doesn’t allow to choose
a password, generating a random one using [wp_generate_password()](https://codex.wordpress.org/Function_Reference/wp_generate_password).
If you want to create a new user with a specific password or with additional parameters,
use [wp_create_user()](https://codex.wordpress.org/Function_Reference/wp_create_user)
or [wp_insert_user()](https://developer.wordpress.org/reference/functions/wp_insert_user/)
instead.

**[register_new_user()](https://developer.wordpress.org/reference/functions/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()](https://developer.wordpress.org/reference/functions/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()](https://codex.wordpress.org/Function_Reference/wp_new_user_notification).
In case of registration failure it returns a [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)().

[register_new_user()](https://developer.wordpress.org/reference/functions/register_new_user/)
also provides two useful hooks to customize validation rules or user registration
process, [register_post](https://codex.wordpress.org/Plugin_API/Action_Reference/register_post)
and [registration_errors](https://codex.wordpress.org/Plugin_API/Filter_Reference/registration_errors).

## 󠀁[Source](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#source)󠁿

    ```php
    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.' ),
    				esc_url( 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&hellip; please contact the <a href="mailto:%s">site admin</a>!' ),
    				esc_attr( 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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/user.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/user.php#L3549)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/user.php#L3549-L3663)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#hooks)󠁿

 [apply_filters( ‘illegal_user_logins’, array $usernames )](https://developer.wordpress.org/reference/hooks/illegal_user_logins/)

Filters the list of disallowed usernames.

 [do_action( ‘register_new_user’, int $user_id )](https://developer.wordpress.org/reference/hooks/register_new_user/)

Fires after a new user registration has been recorded.

 [do_action( ‘register_post’, string $sanitized_user_login, string $user_email, WP_Error $errors )](https://developer.wordpress.org/reference/hooks/register_post/)

Fires when submitting registration form data, before the user is created.

 [apply_filters( ‘registration_errors’, WP_Error $errors, string $sanitized_user_login, string $user_email )](https://developer.wordpress.org/reference/hooks/registration_errors/)

Filters the errors encountered when a new user is being registered.

 [apply_filters( ‘user_registration_email’, string $user_email )](https://developer.wordpress.org/reference/hooks/user_registration_email/)

Filters the email address of a user being registered.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#related)󠁿

| Uses | Description | 
| [get_available_languages()](https://developer.wordpress.org/reference/functions/get_available_languages/)`wp-includes/l10n.php` |

Gets all available languages based on the presence of *.mo and *.l10n.php files in a given directory.

  | 
| [is_email()](https://developer.wordpress.org/reference/functions/is_email/)`wp-includes/formatting.php` |

Verifies that an email is valid.

  | 
| [sanitize_user()](https://developer.wordpress.org/reference/functions/sanitize_user/)`wp-includes/formatting.php` |

Sanitizes a username, stripping out unsafe characters.

  | 
| [wp_generate_password()](https://developer.wordpress.org/reference/functions/wp_generate_password/)`wp-includes/pluggable.php` |

Generates a random password drawn from the defined set of characters.

  | 
| [wp_login_url()](https://developer.wordpress.org/reference/functions/wp_login_url/)`wp-includes/general-template.php` |

Retrieves the login URL.

  | 
| [wp_create_user()](https://developer.wordpress.org/reference/functions/wp_create_user/)`wp-includes/user.php` |

Provides a simpler way of inserting a user into the database.

  | 
| [validate_username()](https://developer.wordpress.org/reference/functions/validate_username/)`wp-includes/user.php` |

Checks whether a username is valid.

  | 
| [username_exists()](https://developer.wordpress.org/reference/functions/username_exists/)`wp-includes/user.php` |

Determines whether the given username exists.

  | 
| [email_exists()](https://developer.wordpress.org/reference/functions/email_exists/)`wp-includes/user.php` |

Determines whether the given email exists.

  | 
| [update_user_meta()](https://developer.wordpress.org/reference/functions/update_user_meta/)`wp-includes/user.php` |

Updates user meta field based on user ID.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [sanitize_text_field()](https://developer.wordpress.org/reference/functions/sanitize_text_field/)`wp-includes/formatting.php` |

Sanitizes a string from user input or from the database.

  | 
| [esc_url()](https://developer.wordpress.org/reference/functions/esc_url/)`wp-includes/formatting.php` |

Checks and cleans a URL.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [do_action()](https://developer.wordpress.org/reference/functions/do_action/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to an action hook.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  | 
| [WP_Error::__construct()](https://developer.wordpress.org/reference/classes/wp_error/__construct/)`wp-includes/class-wp-error.php` |

Initializes the error.

  |

[Show 14 more](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/register_new_user/?output_format=md#comment-content-755)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/register_new_user/#comment-755)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_new_user%2F%23comment-755)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_new_user%2F%23comment-755)
 4. **Example**
     As used in _wp-login.php_:
 5.     ```php
        $user_login = sanitize_text_field( $_POST['user_login'] );
        $user_email = sanitize_email( $_POST['user_email'] );
        $user = register_new_user( $user_login, $user_email );
        if ( ! is_wp_error( $user ) ) {
        	$redirect_to = ! empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
        	wp_safe_redirect( $redirect_to );
        	exit();
        }
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_new_user%2F%3Freplytocom%3D755%23feedback-editor-755)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_new_user%2F)
before being able to contribute a note or feedback.