Wordpress: Send a notification when a user registers, but only for users with Subscriber role

1k views Asked by At

I'm trying to send have my site send me a notification email when someone registers with the role subscriber. I can probably achieve that with a hook on mu-plugins but I don't know where to start or which hook to use. Tried adding an if statement inside a plugin but it's probably overkill to install a plugin and modify it just for this functionality. Thanks!

2

There are 2 answers

0
Sharanjit Singh On BEST ANSWER

For your information the default register user role is "Subscriber" role, if you have set the other role you have to change the role after registered. Try this one:

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

            // email will send only for "Subscriber" registers
            if ( in_array( 'subscriber', $user->roles )) {
              $to = $user_email;
              $subject = "Hi";
              $body = '
                        <p>your message </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 "Other Role" registers
            if ( in_array( 'other_role', $user->roles )) {
              $to = $user_email;
              $subject = "Hi";
              $body = '
                        <p>your message </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');
1
Sharanjit Singh On

It is enabled by default in WordPress. Check that WordPress is sending emails at all, you might have problem with the SMTP settings, you need to configure the SMTP. Where is your website hosted? Did you check the spam?