Forked from dparker1005/get_birthday_at_checkout.php
Created
October 23, 2025 13:09
-
-
Save kimwhite/a51effc35ebe6d3135603bdb479a97d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Add birthday (month and day only) to user signup. | |
| */ | |
| // Add birthday fields at checkout. | |
| function my_pmpro_birthday_checkout_after_email() { | |
| $current_year = date("Y"); | |
| ?> | |
| <div> | |
| <label for="birthday_month">Birthday</label> | |
| <select name="birthday_month" required> | |
| <option value="">Month</option> | |
| <?php | |
| for ( $i = 1; $i <= 12; $i++ ) { | |
| $selected = ( ! empty( $_REQUEST['birthday_month'] ) && intval( $_REQUEST['birthday_month'] ) === $i ) ? 'selected' : ''; | |
| echo '<option value="' . esc_attr( $i ) . '" ' . $selected . '>' . esc_html( date( "M", strtotime( $i . "/15/" . $current_year ) ) ) . '</option>'; | |
| } | |
| ?> | |
| </select> | |
| <input type="number" name="birthday_day" min="1" max="31" placeholder="Day" value="<?php echo ! empty( $_REQUEST['birthday_day'] ) ? intval( $_REQUEST['birthday_day'] ) : ''; ?>" required /> | |
| <small>month/day</small> | |
| </div> | |
| <?php | |
| } | |
| add_action( 'pmpro_checkout_boxes', 'my_pmpro_birthday_checkout_after_email' ); | |
| // Validate birthday input. | |
| function my_pmpro_birthday_registration_checks( $okay ) { | |
| if ( ! $okay ) { | |
| return $okay; | |
| } | |
| global $pmpro_msg, $pmpro_msgt; | |
| $birthday_month = intval( $_REQUEST['birthday_month'] ); | |
| $birthday_day = intval( $_REQUEST['birthday_day'] ); | |
| if ( empty( $birthday_month ) || empty( $birthday_day ) ) { | |
| $pmpro_msg = 'Please enter your birth month and day.'; | |
| $pmpro_msgt = 'pmpro_error'; | |
| return false; | |
| } | |
| return $okay; | |
| } | |
| add_filter( 'pmpro_registration_checks', 'my_pmpro_birthday_registration_checks' ); | |
| // Save birthday to user meta. | |
| function my_pmpro_birthday_after_checkout( $user_id ) { | |
| if ( ! empty( $_REQUEST['birthday_month'] ) && ! empty( $_REQUEST['birthday_day'] ) ) { | |
| $birthday_month = str_pad( intval( $_REQUEST['birthday_month'] ), 2, '0', STR_PAD_LEFT ); | |
| $birthday_day = str_pad( intval( $_REQUEST['birthday_day'] ), 2, '0', STR_PAD_LEFT ); | |
| // Store in MM-DD format. | |
| $birthday = $birthday_month . '-' . $birthday_day; | |
| update_user_meta( $user_id, 'birthday', $birthday ); | |
| } | |
| } | |
| add_action( 'pmpro_after_checkout', 'my_pmpro_birthday_after_checkout' ); | |
| // Add birthday to Members List columns. | |
| function my_pmpro_members_list_heading_birthday( $columns ) { | |
| $columns['birthday'] = 'Birthday'; | |
| return $columns; | |
| } | |
| add_filter( 'pmpro_memberslist_extra_cols', 'my_pmpro_members_list_heading_birthday' ); | |
| // Display the stored birthday. | |
| function my_pmpro_members_list_user_birthday( $user ) { | |
| $birthday = get_user_meta( $user->ID, 'birthday', true ); | |
| $user->birthday = ! empty( $birthday ) ? esc_html( $birthday ) : ''; | |
| return $user; | |
| } | |
| add_filter( 'pmpro_members_list_user', 'my_pmpro_members_list_user_birthday' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment