This post is a quick technical reference for WordPress web developers.
Website developers are often asked to add certain metadata to WordPress blog posts. Most commonly, that metadata is the author’s name and the post’s publication date.
Most WordPress themes have some kind of templated approach for accomplishing this. Or, website owners choose to install plugins that make it easier to manage and add post metadata.
Sometimes, however, neither of these options are suitable (or appealing). Recently I created a way to incorporate the Author and Last Modified Date using a single shortcode.
Best of all, this can be done without installing a(nother) plugin!
How to get the Author name
The first step to adding the author name to a WordPress posts is to use this handy function. It will return the author’s name for the current post, or a given post_id that can be passed in as a function argument. The name is pulled from the list of authenticated website Users, where the user is the one who originally created the post.
How to get the Last Updated Date
This function is pretty self-explanatory. For a given post_id, the most recent modified date is retrieved and returned in date format.
Date formatting in PHP
Using PHP, it’s easy to display date formats in many ways. Here are some examples which can be used in conjunction with the aforementioned
// To show: 12:49:00 PM
echo date('h:i:s P');
// To show: Monday, 11:04:09 AM
echo date('l, h:i:s A');
// To show: 10 January 2020, 08:45:00 PM
echo date('d F Y, h:i:s P');
Once you have one of these functions in your theme’s functions.php file, simply add the corresponding shortcode into your WordPress blog post or page content.
Want to see this in action? Simply scroll to the top of this page.