0

I need to create a custom registration email for different roles that a user signs up to. Here's what I've done so far, a lot if it are copied as I'm still learning:

/*
 * Plugin Name: Custom Registration Email
*/ 

if ( !function_exists('wp_new_user_notification', 'wp_new_user_notification_email') ) {
    function send_welcome_email_to_new_user($user_id) {
            $user_data = get_userdata($user_id);
            $user_email = $user_data->user_email;

            // email will send only for "A" registers
            if ( in_array( 'subscriber', $user_data->roles, true )) {
              $to = $user_email;
              $subject = "Hi A, welcome to our site!";
              $body = '
                        <h1>Dear A test,</h1></br>
                        <p>Thank you for joining our site. Your account is now active.</p>
              ';
              $headers = array('Content-Type: text/html; charset=UTF-8');
              if (wp_mail($to, $subject, $body, $headers)) {
                error_log("email has been successfully sent to user whose email is " . $user_email);
              }
            }

            // email will send only for "B" registers
            if ( in_array( 'free_5ac5f171bdabf209646657', $user_data->roles, true )) {
              $to = $user_email;
              $subject = "Hi Free, welcome to our site!";
              $body = '
                        <h1>Dear Free test,</h1></br>
                        <p>Thank you for joining our site. Your account is now active.</p>
              ';
              $headers = array('Content-Type: text/html; charset=UTF-8');
              if (wp_mail($to, $subject, $body, $headers)) {
                error_log("email has been successfully sent to user whose email is " . $user_email);
              }
            }

            }
}

The issue with it is that it would send 2 emails to the user. The first email is the custom email shown in my code. The second email sent is the default email from WordPress giving the username and the link to create a password.

What I want is to stop the default email from sending. Only the custom message should send, and also to put the username and the create password link inside my custom email message.

To give you a brief background: a user would register in our form, fill their details in and select from the drop down list the package (role) they want. Once finished, they should receive the email for that particular package (role) containing the link to create a password.

*Also, is there a way to edit the role_id? As you can see in my code that the 'Free' package(role) has randomly generated numbers following it. I guess this is done by the theme that we bought.

Thank you everybody!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.