// Function to check if today is user's birthday
function is_users_birthday($user_id) {
$user_info = get_userdata($user_id);
$user_birthday = strtotime($user_info->date_of_birth); // Adjust this to your user meta field
$today = strtotime('today');
return date('md', $user_birthday) == date('md', $today);
}
// Function to add role to user
function add_birthday_role_to_user($user_id) {
$user = new WP_User($user_id);
// Add the desired role here
$user->add_role('birthday_role');
}
// Hook into WordPress init action to check birthdays and add role
add_action('init', 'check_birthday_and_add_role');
function check_birthday_and_add_role() {
// Get users
$users = get_users();
foreach ($users as $user) {
if (is_users_birthday($user->ID)) {
add_birthday_role_to_user($user->ID);
}
}
}
programmatically add a user role for users with birthdays in the current month in WooCommerce
// Function to check if user's birthday is in the current month
function is_users_birthday_this_month($user_id) {
$user_info = get_userdata($user_id);
$user_birthday = strtotime($user_info->date_of_birth); // Adjust this to your user meta field
$current_month = date('m');
$user_month = date('m', $user_birthday);
return $user_month == $current_month;
}
// Function to add role to user
function add_birthday_role_to_user($user_id) {
$user = new WP_User($user_id);
// Add the desired role here
$user->add_role('birthday_role');
}
// Hook into WordPress init action to check birthdays and add role
add_action('init', 'check_birthday_and_add_role');
function check_birthday_and_add_role() {
// Get users
$users = get_users();
foreach ($users as $user) {
if (is_users_birthday_this_month($user->ID)) {
add_birthday_role_to_user($user->ID);
}
}
}