About Me

Display your Age Using a Shortcode

| 1 Comment

I was getting tired of either being generic about my age (twentysomething), or having to update it every year in my About pages. So instead, I set about creating a shortcode that would automatically update my age every year. Here is the snippet that you need to drop into your functionality plugin:

// DISPLAY YOUR AGE //

function whats_my_age_again($birthday) {
	extract( shortcode_atts( array(
		'birthday' => 'birthday'
	), $birthday));
	return date("Y", time() - strtotime($birthday)) - 1970;
}

add_shortcode('show-my-age', 'whats_my_age_again');

Now, in order to display your age, you just need to paste the shortcode in to your page, using the ‘birthday’ attribute to tell WordPress when you were born. For example, for me, it would look like this:

I am [show-my-age birthday="02/19/1985"] years old.

Aside from showing you that it’s my birthday soon, (feel free to send me some chocolates), this shortcode will do the leg work for you and update your age automatically. And then, if you’re feeling a little bit old, you can just tweak your shortcode and instantly make yourself feel better. Ta da!

Categories: Code & Snippets | Permalink

What next?

Hire me

If you couldn't quite manage this yourself, find it too intimidating, or just don't have the time to do it, you can always hire Dave to do it. Please get in touch so that we can discuss your needs.

Leave a comment

If you have a question, update, or comment about the tutorial, please leave a comment. I try and respond to every comment, though it may take a few days, so please check back soon.

Have a WordPress Expert ready to help you

WordPress can do a lot of things and with that comes a steep learning curve. That's why it's nice to have a WordPress expert on hand to help you out when you need it. My service, The WP Butler, gives you the peace of mind that if something goes wrong, I'll be there to help you fix it. Better than that, I'll even keep your site backed up, updated and secure, so that you don't have to worry about it. It's all part of the service. Use coupon DIWW and save 15% on all plans.. Better yet, get a coupon for 25% off by signing up to our newsletter above.

The WP Butler - Your WordPress Expert

Author: Dave Clements

Dave Clements has been building websites for close to a decade and in 2010, he formalised that by starting his own company, The UK Edge. He now works on a variety of web projects, from simple tasks like installing a new WordPress site, to consulting on problems, or redesigning his clients' sites. He also runs Do It With WordPress, a site dedicated to providing free tutorials on WordPress. When he's not building your new website, you can find Dave eating Wheat Thins, spending time with friends and family, watching Indie films, fostering kittens from the local Humane Society, listening to some dubstep, dance and electronic rock, and exploring the world.

One Comment

  1. The age will occasionally be calculated incorrectly with the calculation you are using.

    See this post for 3 different functions for calculating age: http://spyk3lc.blogspot.com/2012/03/php-get-age-comparison-results-of-3.html

    I used your calculation against the getAge3 function from that page and was able to spot it being wrong when the birthday is the previous or next day from the current date.

Leave a Reply