How to send new user notification emails in WordPress

The wp_new_user_notification function in WordPress is utilized to notify the site administrator and the new user when a new user account is created. This function sends two separate emails: one to the administrator of the WordPress site and another to the newly registered user.

The email sent to the administrator will inform them of the new user registration, while the email sent to the new user will contain their username and password. This function assists in keeping the site administrator informed about new users and providing the necessary login information to new users.

The wp_new_user_notification function is part of WordPress’s user management system, which is responsible for managing user registrations, profiles, and roles.

Parameters Accepted by wp_new_user_notification Function

The wp_new_user_notification function in WordPress accepts three parameters, as detailed below:

  • $user_id (integer): This required parameter represents the User ID.
  • $deprecated (null): This optional parameter, which has a default value of null, is not utilized as it is a deprecated argument.
  • $notify (string): This is an optional parameter with a default value of an empty string. It determines the type of notification to be triggered. It accepts ‘admin’ or an empty string for admin-only notifications, ‘user’ for user notifications, or ‘both’ for notifications to both the admin and user.

Return Value of wp_new_user_notification Function

The wp_new_user_notification function does not return any value.

Examples

How to Notify Both Admin and User of a New User Registration

In this example, we are using the wp_new_user_notification function to send a notification to both the admin and the new user when a new user registration occurs.

$user_id = wp_create_user( 'newuser', 'password', '[email protected]' );
if( !is_wp_error($user_id) ) {
 wp_new_user_notification( $user_id, null, 'both' );
}

How to Notify Admin Only of a New User Registration

In this example, we are using the wp_new_user_notification function to send a notification to only the admin when a new user registration occurs.

$user_id = wp_create_user( 'newuser', 'password', '[email protected]' );
if( !is_wp_error($user_id) ) {
 wp_new_user_notification( $user_id, null, 'admin' );
}

How to Notify User Only of a New User Registration

In this example, we are using the wp_new_user_notification function to send a notification to only the new user when a new user registration occurs. The third parameter is set to ‘user’.

$user_id = wp_create_user( 'newuser', 'password', '[email protected]' );
if( !is_wp_error($user_id) ) {
 wp_new_user_notification( $user_id, null, 'user' );
}

Note: wp_create_user function is used in these examples to create a new user and return the user ID. The is_wp_error function is used to check if the user creation was successful before attempting to send the notification.

Conclusion

The wp_new_user_notification function in WordPress serves as a valuable tool for managing email notifications related to new user registration. The function primarily sends an email to the new user containing their login information, and optionally, another email to the site administrator to inform them of the new registration. Thus, the wp_new_user_notification function can be used to automate the process of notifying both the user and the administrator about the new user registration, thereby assisting in the efficient management of user accounts on a WordPress site.

Related WordPress Functions