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.
Usage
[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.
Shortcode
function hx_if_updated_shortcode($atts, $content = null) {
// Make the global $post object available inside this function.
global $post;
/**
* Step 1: Ensure we are inside a single post/page type.
* - is_singular() checks if we are viewing a single 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.
* - Using 'U' ensures easy numeric comparison later.
*/
$published = get_the_date('U', $post);
/**
* Step 3: Get the last modified date as a Unix timestamp.
* - 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.
* - Use do_shortcode($content) so that any shortcodes are 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');
How to use
To manually install this shortcode, you can add it to functions.php
in your theme. However, this is dangerous and I recommend using a plugin that allows you to create and manage custom bits of code without changing your theme files, e.g., Code Snippets, WP Code, FluentSnippets, etc.
Note: if you prefer a different prefix, change all instances of hx_ with the prefix of your choice.
Reference
- add_shortcode() – Function | Developer.WordPress.org
- do_shortcode() – Function | Developer.WordPress.org
- is_singular() – Function | Developer.WordPress.org
- get_the_date() – Function | Developer.WordPress.org
- get_the_modified_date() – Function | Developer.WordPress.org
- $post – Global Variable | Developer.WordPress.org
Leave a Reply