Paul Underwood
PaulundSoftware Engineer

Display A Message On Old Wordpress Posts

If your blog has been around for a while then some of your earlier posts may have information on there which is out of date. For example on this blog some of my older posts may have code which has been deprecated, so the code used to work correctly but now if you try it the code won't work anymore. This can be a problem for both the blog owner and the visitor which has come to your blog from a search engine. They would expect the information they find to be correct not knowing that it is actually out of date. You have a choice to can either hope that your visitors understand that older posts may have code which will no longer work correctly, or you can place a disclaimer message on your older posts to let your visitors know that the information here may be out of date. In this tutorial we are going to add a disclaimer message to all your posts which are older than a year to let the visitor know that this information may be out of date.

Displaying The Message

The message we want to display is only going to appear on the single posts so to add this to our theme we need to open the single.php file. Find where it displays the content and above this function add this following code.

$post_age = date('Y') - get_the_time('Y');
if($post_age > 1 ) { ?>
<div class="message">
<p><strong>This was originally published on < ?php the_time('d/m/Y'); ?></strong><br />
The information found here may be out of date.</p>
<?php } ?>

If you are ok with editing PHP files you can try editing the functions.php file to attach a filter to the content. Wordpress allows you to add a filter before the content is displayed so you can change how this is displayed on the page. The above example can be done in the functions.php file by using the following code.

function old_content_message($content) {
      $content = '<div class="message">
              <p><strong>This was originally published on '. the_time('d/m/Y').'</strong><br />
              The information found here may be out of date.</p>

      return $content;

add_filter( 'the_content', 'old_content_message' );

Styling The Message

With the message on the page we need to style this to make it stand out from this content. Open your theme stylesheet and add te following into the CSS file.

.message {
    border: 1px solid #B4D5FE;
    background: #F4F7FF;
    color: #333;
    padding: 5px 8px;
    overflow: hidden;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;

The Result

The message you are left with is a box which looks like this.

This post was originally published on 18/06/2012
The information found here may be out of date.