I'm stuck trying to figure out a function to send an email when a user's role is changed. Example: if a user has a role of subscriber and the admin changes their role to Editor, the user would receive an email stating their role has been changed.
I can't seem to find any plug-ins out there that offer this functionality and I haven't had any luck. I was thinking something along these lines could work:
[[LINK href="http://wordpress.stackexchange.com/questions/12193/send-automatic-mail-to-admin-when-user-member-changes-adds-profile"]]http://wordpress.stackexchange.com/questions/12193/send-automatic-mail-to-admin-when-user-member-changes-adds-profile[[/LINK]]
But tweaked for a user notification vs. admin but my efforts have been unsuccessful thus far. Any help is appreciated.
Gabriel Reguly answers:
Hi Kristin,
I can do that for $25.
Regards,
Gabriel
Gabriel Reguly comments:
Hi Kristin,
In your functions.php file, add this code
function kristin_send_email( $user_id, $new_role ) {
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = 'Role changed';
$message = "Hello " .$user_info->display_name . " your role has changed, now you are " . $role;
wp_mail( $to, $subject, $message);
}
add_action( 'set_user_role', 'kristin_send_email' );
Regards,
Gabriel
Kristin Falkner comments:
Thank you! I tweaked this slightly for my needs but this got me there! You rock.
Luis Abarca answers:
of course
add_action('profile_update', 'on_profile_updated', 10, 2);
function on_profile_updated( $user_id, $old_user_data)
{
$new_user_data = get_userdata($user_id);
// current role
$user = new WP_User( $user_id );
$current_role = $user->roles[0];
// old role
$old_user = new WP_User( $old_user_data->ID );
$old_role = $old_user->roles[0];
// check if new role or level is different from the old one
if ($current_role != $old_role) {
// send the mail
mail( $new_user_data->user_email, 'Subject', 'You are now a ' . $current_role);
}
}
Luis Cordova answers:
Kristin
that should be pretty easy show on github your code of what you have now and we can go from there
I will hint and guide your development, should be easy
thanks
Luis Cordova comments:
I think it goes then
function notify_user_on_update_by_admin(){
global $current_user;
get_currentuserinfo();
$userEmailAddress = ?? ; // here we find the information from user
if (current_user_can( 'administrator' )){// sending emails when admin is updating user profiles
$to = $userEmailAddress;
$subject = 'admin just updated your profile/role';
$message = "the admin has updated your profile with:\n";
foreach($_POST as $key => $value){
$message .= $key . ": ". $value ."\n";
}
wp_mail( $to, $subject, $message);
}
}
Luis Cordova comments:
the real problem is to distinguish
1 where the admin is and which hooks can you trigger
and
2 right in 1 how you get the current user info the admin is modifying, not the info from the admin
so equivalent of
get_currentuserinfo();