Add Custom Email Notifications for WordPress User Registrations
Explanation
Want to send a personalized welcome email to new users when they register on your WordPress site? Here's how you can do it:
- Custom Welcome Email: When someone registers, a special email is sent to them. It includes a friendly message welcoming them to your site. The email is sent using the user's email address and username.
- Add Custom Fields: You can add extra fields to the registration form, like a phone number. This is done by inserting a new input field into the form.
- Save Custom Fields: After a user registers, any extra information they provided, like their phone number, is saved to their profile.
- Admin Notification: The admin also gets a notification email when a new user registers. This email can be customized to include the username and the name of your site.
These steps help you create a more personalized experience for your users and keep you informed about new registrations on your site.
Code
<?php
// Hook into user registration to send a custom email
add_action('user_register', 'wp_dudecom_send_custom_welcome_email', 10, 1);
/**
* Send a custom welcome email to new users upon registration.
*
* @param int $user_id The ID of the newly registered user.
*/
function wp_dudecom_send_custom_welcome_email($user_id) {
// Get user data
$user_info = get_userdata($user_id);
$user_email = $user_info->user_email;
$user_name = $user_info->user_login;
// Set email subject and message
$subject = 'Welcome to Our Website!';
$message = sprintf('Hello %s, welcome to our website! We are glad to have you.', $user_name);
// Set email headers
$headers = array('Content-Type: text/html; charset=UTF-8');
// Send the email
wp_mail($user_email, $subject, $message, $headers);
}
// Hook into the registration form to add custom fields
add_action('register_form', 'wp_dudecom_add_custom_registration_fields');
/**
* Add custom fields to the WordPress registration form.
*/
function wp_dudecom_add_custom_registration_fields() {
?>
<p>
<label for="phone"><?php _e('Phone Number', 'wp-dudecom'); ?><br/>
<input type="text" name="phone" id="phone" class="input" value="<?php echo esc_attr(wp_unslash($_POST['phone'] ?? '')); ?>" size="25" /></label>
</p>
<?php
}
// Hook into user registration to save custom fields
add_action('user_register', 'wp_dudecom_save_custom_registration_fields');
/**
* Save custom registration fields to user meta.
*
* @param int $user_id The ID of the newly registered user.
*/
function wp_dudecom_save_custom_registration_fields($user_id) {
if (isset($_POST['phone'])) {
update_user_meta($user_id, 'phone', sanitize_text_field($_POST['phone']));
}
}
// Hook to modify the admin email notification
add_filter('wp_new_user_notification_email', 'wp_dudecom_custom_admin_notification_email', 10, 3);
/**
* Customize the admin email notification for new user registration.
*
* @param array $wp_new_user_notification_email The email data.
* @param WP_User $user The user object.
* @param string $blogname The name of the blog.
* @return array Modified email data.
*/
function wp_dudecom_custom_admin_notification_email($wp_new_user_notification_email, $user, $blogname) {
$wp_new_user_notification_email['subject'] = sprintf('[%s] New User Registration: %s', $blogname, $user->user_login);
$wp_new_user_notification_email['message'] = sprintf('A new user has registered on your site %s. Username: %s', $blogname, $user->user_login);
return $wp_new_user_notification_email;
}
?>
Instructions
To implement custom email notifications for user registrations in WordPress, follow these steps:
File Location: Add the following code to your theme's functions.php
file or a custom plugin file.
Prerequisites: Ensure you have access to your WordPress site's file system and a basic understanding of PHP.
Implementation Steps:
- Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to navigate to your WordPress installation directory.
- Edit the
functions.php
File: Locate thefunctions.php
file in your active theme's directory (usually found inwp-content/themes/your-theme-name/
). - Insert the Code: Copy the provided code and paste it at the end of the
functions.php
file. Ensure you don't overwrite any existing code. - Save the Changes: After pasting the code, save the
functions.php
file. - Test the Registration Process: Go to your WordPress site's registration page and register a new user to test if the custom email notifications are working as expected.
- Verify Email Delivery: Check the email inbox of the newly registered user to confirm receipt of the welcome email. Also, check the admin email for the notification.
By following these steps, you can enhance user engagement with personalized welcome emails and keep track of new registrations with customized admin notifications.
If you need further assistance or want to explore more advanced functionalities, consider reaching out to the experts at wp-dude.com.