Advertisement
Guest User

Out-of-the-Box - Add User Roles to Email Notification

a guest
Jun 5th, 2019
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. /* Add User Roles to Email Notification */
  2. add_filter('outofthebox_notification', 'user_role_notification', 10, 1);
  3.  
  4. function user_role_notification($notification) {
  5.  
  6.     $additional_recipients = array();
  7.     foreach ($notification['recipients'] as $key => $recipient) {
  8.  
  9.         /* Skip placeholders and emailaddresses */
  10.         if (strpos($recipient, '%') !== false || strpos($recipient, '@') !== false) {
  11.             continue;
  12.         }
  13.  
  14.         /* Check if recipient is an User Role */
  15.         if (is_a(get_role($recipient), 'WP_Role')) {
  16.             $users = get_users('role=' . $recipient);
  17.  
  18.             /* Add all emailaddresses for User Role */
  19.             foreach ($users as $user) {
  20.                 $additional_recipients[] = $user->user_email;
  21.             }
  22.  
  23.             unset($notification['recipients'][$key]);
  24.         }
  25.     }
  26.  
  27.     /* Make sure that there aren't any duplicates in the list of recipients */
  28.     $notification['recipients'] = array_merge($notification['recipients'], $additional_recipients);
  29.     $notification['recipients'] = array_unique(array_map('trim', $notification['recipients']));
  30.  
  31.     return $notification;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement