vapvarun icon

BuddyPress Birthday Reminder Email

vapvarun | PRO | 11/30/23 04:51:11 AM UTC (Edited) | 0 ⭐ | 1705 👁️ | Never ⏰ | []
PHP |

1.32 KB

|

Software

|

0 👍

/

0 👎

// Add this code to your theme's functions.php file or use a custom code plugin
 
function wbcom_send_birthday_reminders() {
    // Get all BuddyPress members
    $bp_members = new BP_User_Query(array('per_page' => -1));
 
    if (!empty($bp_members->results)) {
        foreach ($bp_members->results as $member) {
            // Get the user's birthday from xProfile field
            $birthday = bp_get_profile_field_data(array('field' => 'Birthday', 'user_id' => $member->ID));
 
            // Check if it's the user's birthday
            if ($birthday && date('md', strtotime($birthday)) == date('md')) {
                // It's the user's birthday, send a reminder email
                $to = $member->user_email;
                $subject = 'Happy Birthday!';
                $message = 'Dear ' . $member->display_name . ',\n\nHappy Birthday!';
 
                // Send email
                wp_mail($to, $subject, $message);
            }
        }
    }
}
 
// Schedule the birthday reminder to be sent daily
add_action('init', function () {
    if (!wp_next_scheduled('wbcom_send_birthday_reminders')) {
        wp_schedule_event(time(), 'daily', 'wbcom_send_birthday_reminders');
    }
});
 
// Hook the function to the scheduled event
add_action('wbcom_send_birthday_reminders', 'wbcom_send_birthday_reminders');
 

Comments