Kev Quirk

This post is more than three years old so may contain incorrect information, or opinions I no longer hold.

How To Add An Old Post Notice To WordPress

21 Apr 2020 | ~2 minute read

I like to let people know when they're reading an old post of mine, as it may contain out of date info. This post will show you how to easily add an old post notice to your WordPress site.

Now you could add an old post notice to WordPress with a plugin, such as Old Post Notification. However, I prefer to code features manually rather than relying on plugins. This is because they can introduce unintended vulnerabilities and slow your site down.

Adding an old post notice to WordPress manually

To do this manually, you will need to edit two files within your theme. If you're using a theme that has been created by someone else, I would recommend creating a child theme so that your edits don't get overwritten every time your theme is updated.

The PHP

First thing we will do is download the single.php file for your theme, and add the following code snippet above the while ( have_posts() ) loop.

<?php
    $old_post = 60*60*24*365; // This is a year
    if((date('U')-get_the_time('U')) > $old_post) {
    echo '<div class="old-notice">This post was last updated over a year ago, therefore the contents of this post may be out of date.</div>';
    }
?>

All you need to do is edit the HTML within the <div> to change the contents of your old post notice.

The CSS

If you don't want to add any formatting to your old post notice, you can stop reading this post right here. However, I like to add a bit of colour and padding so that it stands out.

We've already added a class to the <div> so adding formatting will be trivial. The formatting I have added to mine is:

/* Format the old posts notice */
.old-notice {
    padding: 20px 30px;
    background-color: #226997;
    margin-bottom: 1em;
    color: #fff;
}

Posts that are more than a year old will now include a notice that looks something like this:

Conclusion

With a couple of very small edits, you can save yourself having to add yet another plugin to your WordPress site. And you've managed to learn a bit of PHP and CSS along the way.

Better yet, the whole thing is automated! Old post notices will be added automagically as new posts become a year old. Clever, huh?

Reply by email

Want more content?

Say no more, dear reader. Here's three random posts from this blog for you to peruse:

What's the Alternative to Firefox?
18 Feb 2022

Fat Boy Mailing List
04 Sep 2023

A Script to Automate Git Add, Commit & Push
19 Feb 2023

← The one before
Removing Mastodon Webmention From My IndieWeb

Up next →
Why I Have A Blog

Enjoyed this post?

I put a lot of work into maintaining this site and I really enjoy interacting with my readers.

My fuel of choice is coffee, so if you did enjoy this post, or found it in any way useful, I'd appreciate more fuel to keep me going. ❤️

Buy me a coffee