• Hello,

    I’m looking for a snippet that would help me to send an email notification to users when a new role is added to their profile.

    I found a snippet that works fine if I want to change the default role, but if I add another role nothing is sent.

    Here is the snippet I use :

    function notify_user_on_role_change($user_id,$old_user_data,$userdata) {
        // Getting role before update
        foreach($old_user_data->roles as $role):
            $old_role = $role;
        endforeach;
    
        // error_log(print_r($userdata,true)); // debug 
    
        //If we change role send email
        if($old_role != $userdata['role']):
            $user_info = get_userdata( $user_id );
            $to = $user_info->user_email; 
            $subject = "Profile Updated";
            $message = "Hello, your user role is changed to ".$userdata['role']."";
            wp_mail( $to, $subject, $message);
        endif;
    }
    add_action('profile_update','notify_user_on_role_change',10,3);

    Thanks for your help !
    Jennifer

  • The topic ‘Email Notification to new roles’ is closed to new replies.