Dave McCourt thinks some thoughts...

Send admin email on user update

Posted in: Code snippets, WordPress

I recently built a membership site whereby users once signed up, needed to be approved by an administrator. What I needed was for an automatically generated email to be sent to the user notifying them that their membership had been approved. This was a bit more tricky than I thought when an admin is editing another user’s profile. I needed to use the $profileuser global which contains the current user profile (as opposed to the logged in user) and transients, which I’d not used before.


Send an email when a role is approved
Source: http://revelationconcept.com/wordpress-send-admin-notification-user-updates-profile/

function banana__profile_update($user_id, $old_user_data) {
    if (is_admin()) {
       // $old_user_data only contains a limited set of the user object vars
       $new_user_data = new WP_User($user_id);
       $old_user_role = get_transient('banana__old_user_data_' . $user_id);
       // Roles are stored as array. It seems the keys change, so can't just do $new_user_data->roles[0]
       foreach($new_user_data->roles as $role) {
          $new_user_role = $role;

       // Check if a user role is changed from 'instructor_pending' to 'instructor'
       if ($old_user_role == 'member_pending' && $new_user_role == 'member') {
          $from_email = get_field('ACF_field_key', 'option');
          $body = "Dear " . $new_user_data->user_firstname . "\n\rYour membership to the MEMBER SITE has now been approved. You can login to your account here: https://www.example.com/members/\r\n\r\nThank you\r\n\r\nMEMBER SITE TEAM\r\n\r\nhttps://www.example.com\r\n\r\n";
          $sent = wp_mail($new_user_data->user_email, 'Your membership has been approved', strip_tags($body), '', '-f ' . $from_email);

// Save old user data and meta for later comparison for non-standard fields (role etc)
function banana__old_user_data_transient() {
    if (is_admin()) {
       // Global var of user being edited
       global $profileuser;
       // var_dump($profileuser);
       $user_data = new WP_User($profileuser->ID);
       foreach($user_data->roles as $role) {
          $user_role = $role;

       // 1 hour should be sufficient
       set_transient('banana__old_user_data_' . $profileuser->ID, $user_role, 60 * 60);
// Cleanup transients when done
function banana__old_user_data_cleanup($user_id, $old_user_data){

    delete_transient('banana__old_user_data_' . $user_id);