Skip to content

Instantly share code, notes, and snippets.

@louson21
Last active July 18, 2024 16:07
Show Gist options
  • Save louson21/44bad25a57901eeee64bd81b76875792 to your computer and use it in GitHub Desktop.
Save louson21/44bad25a57901eeee64bd81b76875792 to your computer and use it in GitHub Desktop.
Get age WordPress shortcode with birthdate parameter
/*
* Get the age automatically with the given birth date.
*/
function getAge( $atts = [] ) {
$atts = array_change_key_case( (array) $atts, CASE_LOWER );
$age_atts = shortcode_atts(
array(
'birthdate' => 'January 1, 1970',
), $atts
);
/* Based on David Miles code: https://www.facebook.com/groups/916625091796611 */
$age = date_diff(date_create($age_atts['birthdate']), date_create('now'))->y;
return $age;
}
function getAge_init() {
add_shortcode( 'get-age', 'getAge' );
}
add_action( 'init', 'getAge_init' );
// Put the shortcode like this on the text editor: [get-age birthdate="January 01, 1970"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment