How Old Am I WordPress plugin

How Old Am I is a plugin for WordPress, which calculates and displays your age in several formats, giving you up-to-date detailed age information which can be added on your posts and pages, without having to constantly update them.

You can download it either from the WordPress plugins directory or from GitHub.

For any feedback or issues, feel free to post a comment or contact me directly.

Screenshots


Example of usage: Default settings


Example of usage: Custom format with age in numbers


Example of usage: Custom format with age in full


The plugin’s settings

Main photo by wagaboy

  • Martijn

    Hi,
    I love your simple approach. I have a small question, is it easy to add a parameter like “onpost” to give the age on the day of posting?
    I would like to add this to every post (or maybe put this in the theme, or other position?)
    Regards,
    Martijn

    • Stathis

      Hello Martijn,

      From what I can understand, you want to specify a parameter in the plugin, so it will always show a specific age, regardless of the current date?

      For example, let’s say that I specify 1/1/1980 as my birthday. If today is 1/1/2030, and I use the plugin as it is now, it will display something like “50 years old”.

      But, if I use it specifying a date when I call the plugin (e.g. [how-old-am-i on="01-01-2020"]), it will override today’s date, and it will display “40 years old”.

      I’m not sure if it can be done without specifying a specific date when the plugin is called (and instead grabbing the date from the post).

      Let me know if the above is similar to what you are after (if not, please explain using a few use cases), and I’ll try to include something like that in the plugin.

  • Martijn

    Hi Stathis,
    I added your script a little bit (and dirty ;-)) I wanted to see the age my son is at the day of posting.
    I modified it like this, maybe you can clean it up and make it better:

    function displayAge($atts)
    {
    $options = $this->getAdminOptions();

    $returnValue = ”;
    $onpost = get_the_date();

    extract( shortcode_atts( array(
    ‘onpost’ => ‘no onpost’
    ), $atts));

    if($options[‘php’]===’latest’)
    {
    $birthDate = new DateTime();
    $birthDate->setDate($options[‘birthYear’], $options[‘birthMonth’], $options[‘birthDay’]);
    $now = new DateTime(“now”);
    if ($onpost!=’no onpost’)
    {
    $now->setDate(get_the_date(‘Y’),get_the_date(‘m’),get_the_date(‘d’));
    }
    ….

    • Martijn

      however, i figured that it would be better to edit the theme

      • Stathis

        Thanks for the ideas. :)

        I added a couple of shortcode attributes, and uploaded a new version of the plugin.

  • Eric

    Hi,

    Thanks for this plugin, I also use it to show the age of my daughter as the day of the post. But I have some posts before her birth. That show also the right days and months, but should be red and with an “-” before. Is that possible?

    Kind regards

    Eric

    • http://burnmind.com/ StathisG

      Hi Eric,

      Can you please provide more information? How to you add the shortcode to each post? Manually, or via a script? Can you share an example?

      • Eric

        Hi,

        I added [how-old-am-i on="post"] with the plugin “Simple
        Custom Content Adder” to every post, that works well, but don’t shows
        the posts before the birthday with a minus.

        My site is
        password protected because private fotos of our family, but if you need,
        I could send you access data by e-mail, thanks!

        • http://burnmind.com/ StathisG

          Here’s a simple fix for your case until I find some free time to implement the feature properly (so it can be turned on and off, display as “-” in case of number view or “minus” in case of full text view, etc):

          You can download a modified version of the main file from here. Just replace your version with that.

          In case you want to apply the change manually (it’s very quick and simple), open how-old-am-i.php, navigate at the end of the displayAge function, and put the following code just before line 175, i.e. the return value at the end of the function:

          
          if($birthDate > $now) {
              $returnValue = '-' . $returnValue;
          }
          
          
          
          • Eric

            Great! Thanks, that works, I use this now with = $returnValue . ‘ before birth’

            • http://burnmind.com/ StathisG

              Nice! :)

  • Lonesome Gun

    All i get is Fatal Error…

  • DS

    I’m not sure if you’re continuing support of this plugin. What I’d like to be able to do is: when age is listed as “X” years “X” months, if the age is under 1, then not display the years. I currently have a post that displays as “0 years 2 months”.

    • http://stathisg.com/ Stathis Goudoulakis

      Hi, I haven’t touched it for years, but I might be able to help you with that. :)

      Could you please let me know which format you’re using (i.e. one of the presets or a custom one)?

      Let me know and I’ll have a look at the code in case I can give you a quick fix to apply.

      • DS

        Custom – Years, Months, Days

        I’m %years%, %months%, and %days%

        • http://stathisg.com/ Stathis Goudoulakis

          I had a look, but I don’t think I can provide a quick fix that will completely solve your issue.

          For example, you can put something something like the following in the else if statement starting in line 117 (https://github.com/stathisg/how-old-am-i/blob/master/how-old-am-i.php)—note that I haven’t tested the below.

          
          if($yearsOld === 0) {
              $yearsOld = '';
          }
          

          This will make the %years% part display nothing (keep it mind that it will probably break %w-years%), but I think that will most likely look odd.

          For example, the format you posted in your last example (“I’m %years%, %months%, and %days%”) will display the following for 0 years, 2 months, and 15 days:

          “I’m , 2, and 15”

          For a “%years% years %months% months” format, it will display:

          ” years 2 months”.

          A proper solution would require adding the ability for conditionals in order to wrap accompanying text, etc. but that will require some time to do properly. There might be something simpler to do, so if you have any other idea feel free to share it. :)