The plugin shows the birthday field in My Account → Account details when the Birthday earning action is enabled. This snippet also adds it to the WooCommerce registration form, so customers can enter their birthday when they create an account. This works in both Points and Rewards for WooCommerce and Loyalty Program for WooCommerce.
The Snippet
Add the snippet to your theme’s functions.php or a code snippets plugin.
add_action('woocommerce_register_form', function () {
$value = isset($_POST['wpgl_birthday']) ? sanitize_text_field(wp_unslash($_POST['wpgl_birthday'])) : '';
woocommerce_form_field('wpgl_birthday', [
'type' => 'date',
'label' => __('Birthday', 'wpgens-loyalty-program'),
'required' => false,
'custom_attributes' => ['max' => current_time('Y-m-d')],
], $value);
});
add_action('woocommerce_register_post', function ($username, $email, $errors) {
$birthday = isset($_POST['wpgl_birthday']) ? sanitize_text_field(wp_unslash($_POST['wpgl_birthday'])) : '';
if ($birthday === '') {
return;
}
$date = DateTime::createFromFormat('Y-m-d', $birthday);
if (!$date || $date->format('Y-m-d') !== $birthday || $birthday > current_time('Y-m-d')) {
$errors->add('wpgl_birthday', __('Please enter a valid birthday.', 'wpgl-birthday'));
}
}, 10, 3);
add_action('woocommerce_created_customer', function ($customer_id) {
$birthday = isset($_POST['wpgl_birthday']) ? sanitize_text_field(wp_unslash($_POST['wpgl_birthday'])) : '';
$date = $birthday ? DateTime::createFromFormat('Y-m-d', $birthday) : false;
if (!$date || $date->format('Y-m-d') !== $birthday) {
return;
}
update_user_meta($customer_id, '_wpgens_loyalty_birthday', $birthday);
// Sync to the plugin's email marketing integrations (Klaviyo, Brevo, ...).
do_action('wpgens_loyalty_birthday_saved', $customer_id, $birthday);
});
How It Works
The field is optional. Dates in the future or invalid dates are rejected with an error message. The birthday is saved to the same user field the plugin uses, _wpgens_loyalty_birthday in YYYY-MM-DD format, so the birthday reward works exactly as if the customer had entered it in their account.
After saving, the snippet also sends the birthday to the plugin’s email marketing integrations, such as Klaviyo, Brevo, Omnisend and Mailchimp.
The “Birthday” label uses the plugin’s own translation, so it is already translated in the languages the plugin ships with.
The field is added to the registration form on the My Account page. Accounts created during checkout are not covered. Those customers can add their birthday later in My Account → Account details.