Snippet

Add Custom Email Notifications for WordPress User Registrations

How to send custom email to new users in wordpressWordpress custom email notification for user registrationSet up custom welcome email wordpressWordpress new user registration email customizationAdd custom fields to wordpress user registration formCustomize wordpress new user registration emailWordpress plugin for custom user registration emailHow to change admin email notification in wordpressCreate custom wordpress user registration formWordpress new user email notification setup

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

1<?php 2// Hook into user registration to send a custom email 3add_action('user_register', 'wp_dudecom_send_custom_welcome_email', 10, 1); 4 5/** 6 * Send a custom welcome email to new users upon registration. 7 * 8 * @param int $user_id The ID of the newly registered user. 9 */ 10function wp_dudecom_send_custom_welcome_email($user_id) { 11 // Get user data 12 $user_info = get_userdata($user_id); 13 $user_email = $user_info->user_email; 14 $user_name = $user_info->user_login; 15 16 // Set email subject and message 17 $subject = 'Welcome to Our Website!'; 18 $message = sprintf('Hello %s, welcome to our website! We are glad to have you.', $user_name); 19 20 // Set email headers 21 $headers = array('Content-Type: text/html; charset=UTF-8'); 22 23 // Send the email 24 wp_mail($user_email, $subject, $message, $headers); 25} 26 27// Hook into the registration form to add custom fields 28add_action('register_form', 'wp_dudecom_add_custom_registration_fields'); 29 30/** 31 * Add custom fields to the WordPress registration form. 32 */ 33function wp_dudecom_add_custom_registration_fields() { 34 ?> 35 <p> 36 <label for="phone"><?php _e('Phone Number', 'wp-dudecom'); ?><br/> 37 <input type="text" name="phone" id="phone" class="input" value="<?php echo esc_attr(wp_unslash($_POST['phone'] ?? '')); ?>" size="25" /></label> 38 </p> 39 <?php 40} 41 42// Hook into user registration to save custom fields 43add_action('user_register', 'wp_dudecom_save_custom_registration_fields'); 44 45/** 46 * Save custom registration fields to user meta. 47 * 48 * @param int $user_id The ID of the newly registered user. 49 */ 50function wp_dudecom_save_custom_registration_fields($user_id) { 51 if (isset($_POST['phone'])) { 52 update_user_meta($user_id, 'phone', sanitize_text_field($_POST['phone'])); 53 } 54} 55 56// Hook to modify the admin email notification 57add_filter('wp_new_user_notification_email', 'wp_dudecom_custom_admin_notification_email', 10, 3); 58 59/** 60 * Customize the admin email notification for new user registration. 61 * 62 * @param array $wp_new_user_notification_email The email data. 63 * @param WP_User $user The user object. 64 * @param string $blogname The name of the blog. 65 * @return array Modified email data. 66 */ 67function wp_dudecom_custom_admin_notification_email($wp_new_user_notification_email, $user, $blogname) { 68 $wp_new_user_notification_email['subject'] = sprintf('[%s] New User Registration: %s', $blogname, $user->user_login); 69 $wp_new_user_notification_email['message'] = sprintf('A new user has registered on your site %s. Username: %s', $blogname, $user->user_login); 70 71 return $wp_new_user_notification_email; 72} 73?>

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:

  1. Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to navigate to your WordPress installation directory.
  2. Edit the functions.php File: Locate the functions.php file in your active theme's directory (usually found in wp-content/themes/your-theme-name/).
  3. 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.
  4. Save the Changes: After pasting the code, save the functions.php file.
  5. 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.
  6. 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.