Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Created May 20, 2013 12:43
Show Gist options
  • Save strangerstudios/5611994 to your computer and use it in GitHub Desktop.
Save strangerstudios/5611994 to your computer and use it in GitHub Desktop.
Add a birthday field to the Paid Memberships Pro register/checkout page and check that the age is between 13 and 17.
/*
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_email()
{
$current_day = date("j");
$current_month = date("M");
$current_year = date("Y");
?>
<div>
<label for="birthday_month">Birthday</label>
<select name="birthday_month">
<?php
for($i = 1; $i < 13; $i++)
{
?>
<option value="<?php echo $i?>" <?php if(!empty($_REQUEST['birthday_month']) && $i == $_REQUEST['birthday_month']) { ?>selected="selected"<?php } ?>><?php echo date("M", strtotime($i . "/1/" . $current_year))?></option>
<?php
}
?>
</select>
<input type="text" name="birthday_day" size="4" placeholder="day" value="<?php if(!empty($_REQUEST['birthday_day'])) echo intval($_REQUEST['birthday_day']);?>" />
<input type="text" name="birthday_year" size="8" placeholder="year" value="<?php if(!empty($_REQUEST['birthday_year'])) echo intval($_REQUEST['birthday_year']);?>" />
<small>month/dd/yyyy</small>
</div>
<?php
}
add_action("pmpro_checkout_after_email", "my_pmpro_checkout_after_email");
//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 = intval($_REQUEST['birthday_month']);
$birthday_day = intval($_REQUEST['birthday_day']);
$birthday_year = intval($_REQUEST['birthday_year']);
//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");
@aeoz
Copy link

aeoz commented Aug 14, 2014

Hi strangerstudios ,
I'm currently using this on my checkout form for the website I'm working on. I'm just wondering if it's possible to get the data here and add it on email using Pmpro email templates addon.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment