0

I have a website with two user role named “A” and “B” when anyone doing registration with "A" then i want to send a welcome email and when with “B” wanna send different welcome email, is it possible? Thanks in advance

  • We love to help you but without knowing what you've already tried yourself so far and where exactly you are stuck the only valid answer to this question would be: Yes, sure it's possible. – leymannx Apr 25 at 20:45
1

Yes, this is possible. WordPress defines a function named wp_new_user_notification. You can override this function by creating your own version (also named wp_new_user_notification) in your own theme or plugin.

You may wish to start by copying the contents of the existing core version into your own copy. You can now customize the logic for your own needs.

Since this function passes the $user_id as the first argument, you can use get_userdata to obtain more information about the user (for example that user's roles) and then send different content depending on their role.

$user_data = get_userdata( $user_id );
if ( ! empty( $user_data->roles ) ) {
    if ( in_array( 'role_a', $user_data->roles, true ) {
        // custom content for role a
    } elseif ( in_array( 'role_b', $user_data->roles, true ) {
        // custom content for role b
    }
}
  • Hello Jared Cobb, Thanks for your reply, i wrapped your code like this way but I am getting syntax error. would you mind to review the code please? codeshare.io/5NKnlm Note that: i added these two user roles on my website – rasel mahmud Apr 24 '17 at 14:27
  • @raselmahmud lines 2 & 3 needed to be: $user_data = get_userdata($user_id); $user_email = $user_data->user_email; The variable $user needs to be $user_data – Jared Cobb Apr 24 '17 at 18:44
  • i have changed $user_data = get_userdata($user_id); to $user = get_userdata($user_id); on line number 2 but still not working and getting a syntax error on line number 7 – rasel mahmud Apr 24 '17 at 19:26
  • i have updated code here pastebin.com/EBricGkC – rasel mahmud Apr 24 '17 at 20:11
  • i have solved the issue. there was an issue ")" bracket on line 3. that line will be if ( in_array( 'role_a', $user_data->roles, true )) Thanks for your great help, i am so kind of you – rasel mahmud Apr 25 '17 at 3:35
0

i have solved the issue. Thanks for your great help @jared-cobb

            function send_welcome_email_to_new_user($user_id) {
            $user = get_userdata($user_id);
            $user_email = $user->user_email;

            // email will send only for "A" registers
            if ( in_array( 'A', $user->roles )) {
              $to = $user_email;
              $subject = "Hi A, welcome to our site!";
              $body = '
                        <h1>Dear A,</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( 'B', $user->roles )) {
              $to = $user_email;
              $subject = "Hi B, welcome to our site!";
              $body = '
                        <h1>Dear B,</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);
              }
            }

            }
            add_action('user_register', 'send_welcome_email_to_new_user');

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.