Advertisement
vapvarun

BuddyPress Birthday Reminder Email

Nov 29th, 2023 (edited)
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | Software | 0 0
  1. // Add this code to your theme's functions.php file or use a custom code plugin
  2.  
  3. function wbcom_send_birthday_reminders() {
  4.     // Get all BuddyPress members
  5.     $bp_members = new BP_User_Query(array('per_page' => -1));
  6.  
  7.     if (!empty($bp_members->results)) {
  8.         foreach ($bp_members->results as $member) {
  9.             // Get the user's birthday from xProfile field
  10.             $birthday = bp_get_profile_field_data(array('field' => 'Birthday', 'user_id' => $member->ID));
  11.  
  12.             // Check if it's the user's birthday
  13.             if ($birthday && date('md', strtotime($birthday)) == date('md')) {
  14.                 // It's the user's birthday, send a reminder email
  15.                 $to = $member->user_email;
  16.                 $subject = 'Happy Birthday!';
  17.                 $message = 'Dear ' . $member->display_name . ',\n\nHappy Birthday!';
  18.  
  19.                 // Send email
  20.                 wp_mail($to, $subject, $message);
  21.             }
  22.         }
  23.     }
  24. }
  25.  
  26. // Schedule the birthday reminder to be sent daily
  27. add_action('init', function () {
  28.     if (!wp_next_scheduled('wbcom_send_birthday_reminders')) {
  29.         wp_schedule_event(time(), 'daily', 'wbcom_send_birthday_reminders');
  30.     }
  31. });
  32.  
  33. // Hook the function to the scheduled event
  34. add_action('wbcom_send_birthday_reminders', 'wbcom_send_birthday_reminders');
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement