Está procurando uma maneira de enviar notificações por e-mail aos usuários do WordPress quando a função de usuário deles for alterada? Embora provavelmente exista um plug-in para isso, criamos um trecho de código rápido que você pode usar para enviar notificações por e-mail aos membros quando a função de usuário deles for alterada no WordPress.
Instruções:
Tudo o que você precisa fazer é adicionar esse código ao arquivo functions.php do seu tema ou em um plug-in específico do site:
function user_role_update( $user_id, $new_role ) { $site_url = get_bloginfo('wpurl'); $user_info = get_userdata( $user_id ); $to = $user_info->user_email; $subject = "Role changed: ".$site_url.""; $message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role; wp_mail($to, $subject, $message); } add_action( 'set_user_role', 'user_role_update', 10, 2);
Observação: Se esta é a primeira vez que você adiciona trechos de código no WordPress, consulte nosso guia sobre como copiar/colar corretamente trechos de código no WordPress para não danificar seu site acidentalmente.
Se você gostou desse snippet de código, considere dar uma olhada em nossos outros artigos no site, como: 62 melhores temas de blog gratuitos do WordPress e 7 melhores plugins de formulário de contato do WordPress.
how do I use HTML tags here?
I want to make texte bold
$message = “Hello ” .$user_info->display_name . ” your role has changed on “.$site_url.”, congratulations you are now an ” . $new_role;
You could try using this code for that line:
$message = “Hello ” .$user_info->display_name . ” your role has changed on “.$site_url.”, congratulations you are now an ” . $new_role . ““;
Perfect 👍
Thank you so much 👏
You article has help me but it also sends email to new registrations. I want to use your code for two roles that I want custom messages for. Would this code work for that?
The code would need to be modified to check the value of
$new_role
.I used this code but no mail received when I change my role
You may want to check out: https://www.isitwp.com/set-configure-smtp-wordpress/
this one works and does not send email if new register
function user_role_update( $user_id, $new_role ) {
if ($new_role == “customer”) {}
elseif ($new_role == “role1″||”role2”) {
$site_url = get_bloginfo(‘wpurl’);
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = “A sua conta foi validada. Pode agora começar a comprar em 1223.com”;
$message = “welcome ” .$user_info->display_name . “! message .” ;
wp_mail($to, $subject, $message);
}}
add_action( ‘set_user_role’, ‘user_role_update’, 10, 2);
This was super helpful! Thank you!
Hi,
1. Thanks for the snippet – but I want to notify user only when the role of existing user is changed.
Now the script sends an email when new user is added to WP.
2. It would be more accurate to send the role name (now it sends the slug)
Any ideas?
Thanks
Hi did you received the snippet? if possible could you share me the link too? cause im facing the same error of not sending a separate email
Hi, thanks for that snippet. What about sending notification when the role is changed to certain name?
Eg. I want to send the notification when the user role is changed to ‘group-x’…
Thanks and kind regards
M
Saved me a day of research!! Thanks a lot.
Any idea how to solve this if you want the email to be sent only when you change their role, not when you set the role? With this snippet, an email will be sent when you add a new user. An email will also be sent when you update e.g a users firstname (not touching the role).
You could try to use get_userdata and verify if the current user role is different than the new one…
Brilliant! Thanks!
Thanks Raam, glad to hear you like it.