Displaying an “Aged Post” message on posts before a particular date

There are lots of plugins available that will add a message box to the top of your posts if it’s above a certain age – e.g. 2 years old. However, I had a very specific requirement. After updating my site recently, I had to make changes to my posts to get them to fit with the new design. However, with over 1000 posts, it was going to be a long, long job to do them all. So I didn’t. I only did 2013 onwards. So, I wanted to display an appropriate message for any posts old than that – a set date rather than a moving time period.

I added the following to my functions.php to achieve this. Note that I use Bootstrap Shortcodes and used the CLASS for that to add a dismissible message box around the text.

function old_post_message( $content ) {
    if ( is_single() && get_the_time( 'U' ) < strtotime( '1st January 2013' ) ) {
        $content = '<div class="alert alert-info alert-dismissible"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>Due to updates, over time, that have been made to the site and the age of this article, this post may not display correctly. In particular images may be missing or product reviews display incorrectly.</br></br>If this is the case and you\'d particularly like me to fix it, then please reach out to me on <a href="https://twitter.com/DavidArtiss">Twitter</a>.</div>' . $content;
    return $content;

add_filter( 'the_content', 'old_post_message' );

So, to use, add the code to your own functions.php page, changing the date after which the message should appear (and, of course, change the output message and CLASS for your own unique requirements). Otherwise, feel free to use this code yourself, as you feel fit. I’m thinking, longer term, of converting this into a plugin.

Updated: I’ve added a is_single check to the code, as you probably don’t want this appear on the home page.

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: