Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active August 25, 2024 08:12
Show Gist options
  • Save ipokkel/829397eec71c717ef2765affb5244f41 to your computer and use it in GitHub Desktop.
Save ipokkel/829397eec71c717ef2765affb5244f41 to your computer and use it in GitHub Desktop.
Validate a date field submitted. 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_pmpro_registration_checks_birthday( $okay ) {
// Set your date field name here.
$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 ( ( empty( $birthday['y'] ) || $birthday['y'] === $year )
&& ( empty( $birthday['m'] ) || $birthday['m'] === $month )
&& ( empty( $birthday['d'] ) || $birthday['d'] === $day )
) {
pmpro_setMessage( 'Please fill out the Birthday field.', '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