Your cart is currently empty!
This WordPress shortcode allows you to display specific content only when a post or page has been modified after its original publication date. This is useful for calling attention to the fact that changes have occurred. It also provides more flexibility than the built-in Modified Date block, which is capable of selectively hiding itself, but offers no additional formatting options.
[hx_if_updated]
This article was updated since its original publication!
I can also run shortcodes in this block.
[/hx_if_updated]
Note: You can change the hx_ prefix when you install the shortcode. As a best practice, you should use a unique prefix for your shortcodes to avoid conflicts.
function hx_if_updated_shortcode($atts, $content = null) {
// Make the global $post object available inside this function.
// This object contains all the current post's data, including publish and modified dates.
global $post;
/**
* Step 1: Ensure we are inside a single post/page context.
* - is_singular() checks if we are viewing a single post, page, or custom post type.
* - empty($post) ensures the global $post object is available.
* If not, we simply return an empty string — nothing will be output.
*/
if (!is_singular() || empty($post)) {
return ''; // Exit early, no content to show.
}
/**
* Step 2: Get the original publish date as a Unix timestamp.
* - get_the_date('U', $post) returns the publish date in 'U' format (seconds since epoch).
* - Using 'U' ensures easy numeric comparison later.
*/
$published = get_the_date('U', $post);
/**
* Step 3: Get the last modified date as a Unix timestamp.
* - get_the_modified_date('U', $post) returns the last modified date in the same format.
* - If the post has never been updated, this will equal the publish date.
*/
$modified = get_the_modified_date('U', $post);
/**
* Step 4: Compare the two timestamps.
* - If the modified date is greater than the published date,
* it means the post was updated after its original publication.
* - In that case, return the inner shortcode content.
* - We use do_shortcode($content) so that any shortcodes inside are also processed.
*/
if ($modified > $published) {
return do_shortcode($content); // Show content if updated.
}
/**
* Step 5: If the post has not been updated, return an empty string.
* - This ensures nothing is displayed when there’s no update.
*/
return ''; // No output if post hasn't been updated.
}
// Step 6: Register the shortcode with WordPress.
// - The first parameter is the shortcode tag name ('if_updated').
// - The second parameter is the function that will handle the shortcode logic.
add_shortcode('hx_if_updated', 'hx_if_updated_shortcode');
is_singular
to ensure it is running on a single page or post.get_the_date
to get the publication date of the page or post.get_the_modified_date
to get the modification date.do_shortcode
.add_shortcode
.To manually install this shortcode, you can add it to functions.php in your theme. However, this is dangerous and we recommend using a plugin that allows you to create and manage custom bits of code without changing your theme files. Two plugins we recommend are Code Snippets and WPCode. Both have free versions that allow you to create custom shortcodes.
Note: if you prefer a different prefix, change all instances of hx_ with the prefix of your choice. You need to update the name of the function and the parameters to add_shortcode
.
Having trouble?
We complete tasks for you. Our goal is to offload your technical labor so you can focus on business and innovation.
$60
per hour
Fixed rate for all types of tasks.
No monthly charges.
Pay by the minute.
Leave a Reply
You must be logged in to post a comment.