Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created March 25, 2019 21:30
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 tripflex/7a5e6e83879f0910ceaeab16b27c67a0 to your computer and use it in GitHub Desktop.
Save tripflex/7a5e6e83879f0910ceaeab16b27c67a0 to your computer and use it in GitHub Desktop.
Validate a Job/Listing or Resume Date Picker field is 18 years or more in the past (validate 18 years or older) when using WP Job Manager Field Editor
<?php
add_filter( 'submit_job_form_validate_fields', 'smyles_validate_job_birthday_over_18', 10, 3 );
/**
* Validate a Job/Listing Date Field is 18 years or more in the past
*
*
* @param $has_error
* @param $fields
* @param $values
*
* @return bool|\WP_Error
* @throws \Exception
* @since @@version
*
*/
function smyles_validate_job_birthday_over_18( $has_error, $fields, $values ) {
$field_meta_key = "your_birthday";
// Return true if this field doesn't exist (to prevent errors if you dont have field created)
if ( ! isset( $values['job'][ $field_meta_key ] ) ) {
return true;
}
$entered_date = $values['job'][ $field_meta_key ];
// Parse into date object
$parsed_date = new DateTime( $entered_date );
$parsed_date->add( new DateInterval( "P18Y" ) );
// Add 18 years
if ( $parsed_date > new DateTime() ) {
return new WP_Error( 'validation-error', __( 'You must be 18 years or older!' ) );
}
return $has_error;
}
<?php
add_filter( 'submit_resume_form_validate_fields', 'smyles_validate_resume_birthday_over_18', 10, 3 );
/**
* Validate a Resume Date Field is 18 years or more in the past
*
*
* @param $has_error
* @param $fields
* @param $values
*
* @return bool|\WP_Error
* @throws \Exception
* @since @@version
*
*/
function smyles_validate_resume_birthday_over_18( $has_error, $fields, $values ) {
$field_meta_key = "your_birthday";
// Return true if this field doesn't exist (to prevent errors if you dont have field created)
if ( ! isset( $values['resume_fields'][ $field_meta_key ] ) ) {
return true;
}
$entered_date = $values['resume_fields'][ $field_meta_key ];
// Parse into date object
$parsed_date = new DateTime( $entered_date );
// Add 18 years
$parsed_date->add( new DateInterval( "P18Y" ) );
if ( $parsed_date > new DateTime() ) {
return new WP_Error( 'validation-error', __( 'You must be 18 years or older!' ) );
}
return $has_error;
}
@tripflex
Copy link
Author

If you instead want to use an "age" field, use this code (for resumes):
https://gist.github.com/tripflex/db58fdcbb790ff4641f2b1e19c9f8883

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