WordPress: Stop Updating Years On Your With This Shortcode To Calculate Years Since
One of the best features of WordPress as a content management system (CMS) is Shortcodes. The flexibility to program dynamic content using shortcodes can help you deploy robust and incredible sites. Shortcodes are substitution strings you can insert into your content that renders dynamic content.
I’m helping a client this week where they’re taking one of their products and rolling it out into a new domain. The site is hundreds of pages and has been quite an undertaking. As we’ve been working on the hit list of issues, one that popped up was that there were dozens of blog posts, pages, and calls to action that spoke to the company’s years in business.
Some pages had 13 years, some 15 years, and newer content was accurate at 17 years… all depending upon when they were written. This is one of those unnecessary edits to need to make that a shortcode can handle perfectly.
Shortcode for Years Since
All we need to do is register a shortcode that takes the current year and subtracts it from the year the company was established. We can place a default year to always calculate from OR we can pass the year.
A best practice for you would be to build and add a custom plugin for your site to add this shortcode. While you can add shortcodes to your theme, if you’re outputting your site with an AMP plugin, those shortcodes will not render. If you add them to a custom plugin, they will render in AMP pages.
Here’s the function:
function yearssince_shortcode($atts) {
$atts = shortcode_atts(array(
'startdate' => '7/14/2005',
),
$atts
);
$startdate = new DateTime($atts['startdate']);
$today = new DateTime(date('m/d/Y'));
$datediff = $today->diff($startdate);
$yeardiff = $datediff->y;
return $yeardiff;
}
add_shortcode( 'yearssince', 'yearssince_shortcode' );
The function subtracts the current year from the year you pass, or uses the date you enter in this code as the default. In this case, I used the date of the first published post on Martech Zone.
Martech Zone has been published for over [yearssince] years!
The result is:
Martech Zone has been published for over 19 years!
This shortcode will calculate the number of years since that date. As an example, if I wish to write how long how many years ago my birthday was, I’d write:
Douglas Karr is [yearssince startdate="4/19/1968"] years old!
The result is:
Douglas Karr is 56 years old!
Of course, you can get far more complex with this type of shortcode… you could use HTML, images, CSS, etc., but this is just a simple example to ensure your site is already accurate!
How to Package This Into A Custom Plugin
I’d recommend building a custom plugin for your site that incorporates all your shortcodes, even those within your theme. To do this:
- Create a folder that’s uniquely named. For example, ours is named mtz-shortcodes.
- Within that file, add a shortcodes.php file. (You can name it anything you’d like)
- Within the shortcodes.php file, you can add the code above in addition to the plugin information that will display on your plugins page:
<?php
/*
Plugin Name: Martech Zone Shortcodes
Description: Shortcodes for Martech Zone. This way they work with AMP.
Version: 1.0.0
Author: Douglas Karr
Author URI: https://dknewmedia.com
*/
function yearssince_shortcode( $atts ) {
$atts = shortcode_atts(array(
'startdate' => '7/14/2005',
),
$atts
);
$startdate = new DateTime($atts['startdate']);
$today = new DateTime(date('m/d/Y'));
$datediff = $today->diff($startdate);
$yeardiff = $datediff->y;
return $yeardiff;
}
add_shortcode( 'yearssince', 'yearssince_shortcode' );
- Zip up the folder, and you can now upload and activate the plugin via your WordPress plugins menu.
Moving Your Theme Shortcodes
Many themes come with an array of shortcodes built-in as well. If you’d like to transfer those shortcodes to your custom shortcodes plugin and remove them from your theme, you can write code in your custom shortcodes plugin to deactivate the theme shortcode and; instead, register the shortcode with your custom shortcode plugin. Here’s how: