Using WordPress ‘register_new_user()’ PHP function

The register_new_user() WordPress PHP function handles registering a new user.

Usage

$user_login = "johndoe";
$user_email = "[email protected]";
$user = register_new_user($user_login, $user_email);

Parameters

  • $user_login string: Required. User’s username for logging in.
  • $user_email string: Required. User’s email address to send password and add.

More information

See WordPress Developer Resources: register_new_user()

Examples

Basic Registration

Register a new user with a given username and email address.

$user_login = "johndoe";
$user_email = "[email protected]";
$user = register_new_user($user_login, $user_email);

Registration with Redirect

Register a new user and redirect them to a custom page after successful registration.

$user_login = "janedoe";
$user_email = "[email protected]";
$user = register_new_user($user_login, $user_email);

if (!is_wp_error($user)) {
    $redirect_to = "welcome.php";
    wp_safe_redirect($redirect_to);
    exit();
}

Registration with Error Handling

Register a new user and display an error message if the registration fails.

$user_login = "johndoe";
$user_email = "[email protected]";
$user = register_new_user($user_login, $user_email);

if (is_wp_error($user)) {
    echo $user->get_error_message();
} else {
    echo "Registration successful!";
}

Registration with Custom User Role

Register a new user with a custom user role.

$user_login = "johndoe";
$user_email = "[email protected]";
$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');
}

Registration with Additional User Meta

Register a new user and add additional user meta data.

$user_login = "johndoe";
$user_email = "[email protected]";
$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');
}