Last active
January 4, 2017 16:43
-
-
Save tripflex/561029638d83b7f7b98c79418ba644a6 to your computer and use it in GitHub Desktop.
Output Age from custom date value in WP Job Manager Field Editor (requires PHP 5.3+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ^ the <?php above should only be in your functions.php file ONCE, at the top | |
// The filter is field_editor_output_as_value_METAKEY | |
// where you need to replace METAKEY with the actual meta key you want to filter the output for | |
// | |
// Below the example meta key used is "candidate_dob" | |
add_filter( 'field_editor_output_as_value_candidate_dob', 'smyles_output_age_from_dob' ); | |
function smyles_output_age_from_dob( $value ){ | |
// No need to process if empty value (0, false, null, etc) | |
if( empty( $value ) ) { | |
return $value; | |
} | |
// REQUIRES PHP 5.3+ (you should already be using this, if not, you have bigger problems to deal with) | |
// @see http://stackoverflow.com/questions/3776682/php-calculate-age | |
$age = date_diff( date_create( $value ), date_create('now') )->y; | |
return $age; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment