Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active July 1, 2021 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/829397eec71c717ef2765affb5244f41 to your computer and use it in GitHub Desktop.
Save ipokkel/829397eec71c717ef2765affb5244f41 to your computer and use it in GitHub Desktop.
Requires the Register Helper date field to be filled in on checkout. Fails if date is same as today's date or if one of the date fields are left empty to prevent defaulting to epoch Jan 1,1970 if day value was omitted.
<?php
/**
* This recipe checks that a date type Register Helper field had values
* entered.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmprorh_init_date_example_birthday_field() {
// don't break if Register Helper is not loaded
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
// define the fields
$fields = array();
$fields[] = new PMProRH_Field(
'birthday', // input field name, used as meta key
'date', // field type
array(
'label' => 'Birthday', // field label
'profile' => true, // display on user profile
)
);
foreach ( $fields as $field ) {
pmprorh_add_registration_field(
'checkout_boxes', // location on checkout page
$field // PMProRH_Field object
);
}
// that's it. see the PMPro Register Helper readme for more information and examples.
}
add_action( 'init', 'my_pmprorh_init_date_example_birthday_field' );
add_filter( 'pmprorh_section_header', 'more_information_custom' );
function more_information_custom() {
$title = __( 'Custom Title', 'pmpro-register-helper' );
return $title;
}
function my_pmpro_registration_checks_birthday( $okay ) {
global $pmpro_msg, $pmpro_msgt, $current_user;
$birthday = $_REQUEST['birthday'];
$today = strtotime( date( 'Y-m-d' ), current_time( 'timestamp' ) );
$year = date( 'Y', $today );
$month = date( 'n', $today );
$day = date( 'j', $today );
if ( ( $birthday['y'] === $year || empty( $birthday['y'] ) )
&& ( $birthday['m'] === $month || empty( $birthday['m'] ) )
&& ( $birthday['d'] === $day || empty( $birthday['d'] ) )
) {
$pmpro_msg = 'Please fill out the Birthday field.';
$pmpro_msgt = 'pmpro_error';
return false;
}
return $okay;
}
add_filter( 'pmpro_registration_checks', 'my_pmpro_registration_checks_birthday' );
@ipokkel
Copy link
Author

ipokkel commented Jan 7, 2021

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