Skip to content

Instantly share code, notes, and snippets.

@eighty20results
Last active March 9, 2016 22:36
Show Gist options
  • Save eighty20results/2a4b8378bb7f467d88fc to your computer and use it in GitHub Desktop.
Save eighty20results/2a4b8378bb7f467d88fc to your computer and use it in GitHub Desktop.
Collect birthdate on checkout
<?php
/*
Add birthday to user signup and check that user is between 13-17.
Will not work with PayPal Standard or PayPal Express.
*/
//birthday fields
function my_pmpro_checkout_after_user_fields()
{
$current_day = date("j");
$current_month = date("M");
$current_year = date("Y");
global $current_user;
if ( empty($current_user->user_email) )
{
//Check whether birthday field is set for the user checking in
$email = isset($_REQUEST['bemail']) ? $_REQUEST['bemail'] : null;
if (!empty($email))
$user = get_user_by('email', sanitize_email($email));
else
$user = pmpro_set_current_user();
}
else {
$user = $current_user;
}
$bday_date = get_user_meta($user->ID, 'birthday', true);
if (false === $bday_date) {
// birthday info set in form
if (isset($_REQUEST['birthday_month']))
{
$bday = intval( $_REQUEST['birthday_day'] );
$bmonth = intval( $_REQUEST['birthday_month'] );
$byear = intval( $_REQUEST['birthday_year'] );
$bday_date = "{$byear}-{$bmonth}-{$bday}";
}
}
if (empty($bday_date))
$bday_date = "{$current_year}-{$current_month}-{$current_day}";
//Birthday: '1981-01-10' => array( 1981, 01, 10 );
$bday_arr = explode('-', $bday_date);
?>
<div>
<label for="birthday_month">Birthday</label>
<select name="birthday_month">
<?php
for($i = 1; $i < 13; $i++)
{
?>
<option value="<?php echo $i?>" <?php selected($i, $bday_arr[1], true); ?>><?php echo date("M", strtotime($i . "/1/" . $bday_arr[0]))?></option>
<?php
}
?>
</select>
<input type="text" name="birthday_day" size="4" placeholder="day" value="<?php echo $bday_arr[2]; ?>" />
<input type="text" name="birthday_year" size="8" placeholder="year" value="<?php echo $bday_arr[0]; ?>" />
<small>month/dd/yyyy</small>
</div>
<?php
}
add_action("pmpro_checkout_after_user_fields", "my_pmpro_checkout_after_user_fields");
//checking birthday
function my_pmpro_registration_checks($okay)
{
if(!$okay)
return $okay; //there are other errors to handle
global $pmpro_msg, $pmpro_msgt;
$birthday_month = isset($_REQUEST['birthday_month']) ? intval($_REQUEST['birthday_month']) : null;
$birthday_day = isset($_REQUEST['birthday_day']) ? intval($_REQUEST['birthday_day']) : null;
$birthday_year = isset($_REQUEST['birthday_year']) ? intval($_REQUEST['birthday_year']) : null;
//make sure they entered any birthday
if(empty($birthday_month) || empty($birthday_day) || empty($birthday_year))
{
$pmpro_msg = "Please enter a birthday.";
$pmpro_msgt = "pmpro_error";
return false;
}
//make sure birthday is between 13-17
$age = my_getAge($birthday_year . "-" . $birthday_month . "-" . $birthday_day);
if($age < 13 || $age > 17)
{
$pmpro_msg = "You must be between 13 and 17 years old.";
$pmpro_msgt = "pmpro_error";
return false;
}
return $okay;
}
// add_filter("pmpro_registration_checks", "my_pmpro_registration_checks");
//age function #3 from: http://spyk3lc.blogspot.com/2012/03/php-get-age-comparison-results-of-3.html
function my_getAge($date) // Y-m-d format
{
return intval(substr(date('Ymd') - date('Ymd', strtotime($date)), 0, -4));
}
//saving birthday
function my_pmpro_after_checkout($user_id)
{
if(!empty($_REQUEST['birthday_month']))
{
$birthday_month = intval($_REQUEST['birthday_month']);
$birthday_day = intval($_REQUEST['birthday_day']);
$birthday_year = intval($_REQUEST['birthday_year']);
//save birthday
$birthday = $birthday_year . "-" . $birthday_month . "-" . $birthday_day;
update_user_meta($user_id, "birthday", $birthday);
}
}
add_action("pmpro_after_checkout", "my_pmpro_after_checkout");
add_action("pmpro_checkout_before_processing", "my_pmpro_after_checkout");
function my_extra_csv_cols($extra_cols)
{
$extra_cols[] = array('metavalues', 'pmpro_birthday');
return $extra_cols;
}
add_filter('pmpro_members_list_csv_extra_columns', 'my_extra_csv_cols');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment