A WordPress shortcode to show content only if the post has been updated

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.
    // 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');

How it works

  1. The function calls is_singular to ensure it is running on a single page or post.
  2. The function calls get_the_date to get the publication date of the page or post.
  3. The function calls get_the_modified_date to get the modification date.
  4. If the dates are different, the page has been modified, so the function renders the inner content with do_shortcode.
  5. Otherwise the function returns an empty string (ignoring the inner content).
  6. The function is registered as a shortcode with add_shortcode.

How to use

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.

Reference

License

Feel free to remix this content for your website.

Licensed under CC BY 4.0

Leave a Reply