One of the sites we worked with back in 2015 would render the age of a person (or years since an event) in the article body. Since much of the information we share online is timeless in nature it makes sense to always have a dynamic (and always up-to-date) age calculation. The WordPress shortcode function on this page will show you how to include the current age of a person (or years since a any event) with very simple WordPress shortcode. There's a number of these little countdown snippets we'll be sharing over time - this is just the first.
The Result
To display the current age of Jennifer Lawrence we would use the shortcode of [age dob="19900815"]
(15th August, 1990. See below for other date options). The result: 34. If you're somebody that prefers opening and closing tags, consider using [age]19900815[/age]
. Returns: 15th August 1990 (age: 26).
There is a caveat in providing shortcodes that support both the opening and closing tags version: you must use one or the another. Using both on the same page will cause erroneous output to be rendered.
WordPress Shortcode Function
Copy and paste the WordPress function into your theme's functions.php
file or, if you sensibly have one installed, your custom functions plugin.
If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.
While there's a plugin download below, the custom functions option is a far more reasonable one. Our plugin options are usually there to mitigate issues with entry-level WordPress users, and this is one of those examples.
Shortcode Attributes
The following shortcode attributes may be used.
dob
age
tags. Use one method or the other; not both.date
date="1"
. The default means of rendering the date and age is 15th August 1990 (age: 26). Adjust the code as necessary to alter the output.dateformat
dateformat
determines how the date will be written. Refer to the PHP manual for information.PHP Function
Used outside of WordPress, the following can be used.
Date Formats
PHP's strtotime() function recognises only certain patterns and it's easy to confuse date formats because of the backwards way the USA represents the date. The American m/d/y
is assumed with slashes; however, if the separator is a dash (-) or a dot (.), then the European d-m-y
format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD
). In my example above I've used the 6 figure time group of YYYYMMDD
(without any separator). An English representation is okay as well. For example, [age dob="25 December 2005"]
will also work.
Considerations
- Showing a real-time age can often be a little deceiving if shown in the context of a dated page. Many will look to a publication date and make their own age correction (which will, of course, be in error). We've only used the functionality on a platform that showed a current age on a page where publication date was irrelevant so we really haven't encountered our own issues.
- As previously stated, you should use a shortcode version with the closing tags or the one without - don't use both. The shortcode parser is unable to handle mixing of enclosing and non-enclosing forms of the same
[$tag]
. - If you're a client and require this feature, please get in touch.
Video Introduction
Download
Title: Determine A Person's Age From DOB (Shortcode)
Description: Show the current age of a person (or years since a any event) with WordPress shortcode or PHP.
Download • Version 0.1, 578.0B, zip, Category: WordPress Shortcodes
WordPress Plugins (General), (706.0B) PHP Code & Snippets, (427.0B)