close up of computer keyboard

Insert Auto Updating Age Indicator in WordPress

Share this post on:

So I wanted to have a cool bio page on my website, and while I was writing up about myself, I thought, “Hay! wouldn’t it be cool to have your age shown up here but in days format?” and that when I started to look for a way to show age calculator on my WordPress site, and echoing the results in days, instead of old and boring years, months format. And this has to automatically update every day.

I came across a couple of methods and also some plugins to keep ages up-to-date on my WordPress content page, but I rarely use a third-party plugin on my site unless it solves more than one purpose. so I use PHP code embedding in the functions.php file.

The beauty of this is that it is a super minimalistic approach and also has support for shortcodes. Other than this the code runs directly on the server side, so don’t worry if the client browser does not support any javascript or anything.

The Code

You don’t have to be a Coding Geek to do this but there is a small bit of coding involved here, we have to start by adding the following code in the functions.php file of our child theme:

// Add shortcode for age calculation in Days.
function calculate_years_shortcode( $atts ) {
   $years = '';
   // Attributes
   $atts = shortcode_atts(
      array(
         'datestring' => '',
      ),
      $atts
   );
   if ($atts['datestring'] != '') {
      $dob = new DateTime($atts['datestring']);
      $today = new DateTime(date('m/d/Y'));
      $diff = $today->diff($dob);
      $years = $diff->format('%a days');
   }
   return $years;
}
add_shortcode( 'calculate_years', 'calculate_years_shortcode' );

The Shortcode

Now wherever you want to put your age in days, just add a shortcode tag for “calculate_years” into the content with the default Visual Editor of WordPress.

[calculate_years datestring="m/d/yyyy"]

You can also enter the datestring in “yyyy/m/d” format.

The Result

In my case, I wanted to include a sentence that says, “I’ve been breathing on this planet for X Days.” Here X will represent the number of days since September 1, 1986, which happens to be my date of birth. so I just added the following line in the Visual Editor:

I've been breathing on this planet for [calculate_years datestring="9/1/1986"] Days.

I’ve been breathing on this planet for [calculate_years datestring=”9/1/1986″] .

Rupinder Singh

Using the Shortcode In a Widget

Although I just had to use it in the content area only, Some WordPress themes don’t process shortcodes in a text widget out of the box. You may be able to get the shortcode working in widgets by adding the following line to the child theme’s functions.php:

add_filter( 'widget_text', 'do_shortcode' );

Author: Rupinder Singh

I am a tireless intelligence seeker, coincidentally I am a computer guy too, who is passionate about Information Tools and Open-Source software. I Read Books, play Computer Games, Climb Mountains, when I am not changing the code.

View all posts by Rupinder Singh >

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.