How To Add An Old Post Notice To WordPress - Kev Quirk

Kev Quirk|

Menu

How To Add An Old Post Notice To WordPress

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:

Old post notification example

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?

Want to leave a comment?

I have decided to remove the comments from this blog (here's why). If you want to give me some feedback on this post, please contact me. Webmentions from the IndieWeb are displayed below.

«

»