Search
Close this search box.

How to Display Warning for Outdated Articles in WordPress

Displaying warnings for outdated articles can serve as a signal to readers that the content they are about to engage with may no longer reflect current understanding, research, or events. Providing such warnings, platforms or publishers can prevent misinformation and the propagation of outdated or inaccurate data.

By adding a warning to the article indicating that it hasn’t been updated in a while, the website can prompt readers to approach the content with caution and potentially seek more up-to-date information.

This guide will walk you through the process of adding these warnings without the need for installing extra plugins. Let’s dive in!

Displaying Warning for Outdated Articles in WordPress

Step 1: Adding a Custom Code

To begin, let’s incorporate a custom code for displaying warnings on outdated articles into your WordPress website. While there are safer methods available, this tutorial will focus on the most straightforward approach: adding the code to the functions.php file of your currently active theme.

Before proceeding, it’s wise to create a backup of your WordPress files. You can do this through backup plugins or by utilizing the built-in backup feature provided by your hosting service.

To get started, navigate to Appearance → Theme File Editor in your WordPress dashboard. From there, select the functions.php file from the list of theme files. Once you’ve opened the file in the code editor, scroll down to the bottom and paste the following code.

function wpp_outdated_post($content) {
  if ( is_singular() && get_post_type() === 'post' ) {
      $post_date = get_the_time('Y-m-d');
      $modified_date = get_the_modified_time('Y-m-d');
      $current_date = current_time('Y-m-d');
      $outdated_threshold = strtotime('-2 years'); // Define outdated article age
  
      if ( strtotime($post_date) < $outdated_threshold && $post_date === $modified_date) {
        $warning_message = '<div><span class="wpp-outdated-warning"><b>Warning:</b> This article was published on ' . get_the_time( 'M j, Y' ) . '. Some information may be outdated.</span></div>';
        return $warning_message.$content;
      }else if (strtotime($modified_date) < $outdated_threshold && $post_date !== $modified_date){
        $warning_message = '<div><span class="wpp-outdated-warning"><b>Warning:</b> This article last updated on ' . get_post_modified_time( 'M j, Y' ) . '. Some information may be outdated.</span></div>';
        return $warning_message.$content;
      }        
    }
  }
 add_action( 'the_content', 'wpp_outdated_post' );

The provided code will display a warning message, “Warning: This article last updated on [date]. Some information may be outdated,” for articles that haven’t been updated in over two years, and “Warning: This article was published on [date]. Some information may be outdated” for published articles more than two years ago but never have an update.

You can customize the warning message by adjusting the value of the $warning_message variable. Additionally, you can modify the threshold for outdated articles by changing the value of the $outdated_threshold variable to your desired timeframe, such as ” -1 year (1 year ago),” “-30 days (30 days ago),” and so on. Feel free to tailor these settings according to your preferences.

$outdated_threshold = strtotime('-30 days');

Once the code is ready, click the Update File button to save and apply the changes. Now your outdated articles should display a warning just before the content.

The example provided above is for demonstration purposes, so we have set the code’s outdated threshold to 30 days instead of 2 years.

Step 2: Highlight the Warning Message

The next step is to enhance the visibility of the warning by adding custom CSS to make it stand out and catch the user’s attention immediately. You can achieve this by targeting the wpp-outdated-warning custom CSS class assigned to the warning message.

To implement this, navigate to Appearance → Customize and access the Additional CSS section. Then, add the following custom CSS code.

 .wpp-outdated-warning {
  background-color: orange;
  color: black;
  padding: 5px;
  border-radius: 5px;

  /* Add a subtle text shadow*/
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.3);	
}

The provided CSS will emphasize the warning by giving it an orange-colored background and adding a subtle text shadow to enhance readability. Click the Publish button to apply these style changes and start highlighting the warning message.

The Bottom Line

In conclusion, by implementing warnings for outdated articles, platforms and publishers can actively contribute to the prevention of misinformation and the dissemination of inaccurate data. This guide shows you how to add warnings to outdated articles without the need for additional plugins.

This page may contain affiliate links, which help support our project. Read our affiliate disclosure.
Picture of Hendri Risman

Hendri Risman

Hendri is a WordPress expert and a writer staff at WPPagebuilders. He writes solutions on how to get things fixed in WordPress a lot. Mostly without involving a plugin.
Want to turn your WordPress knowledge into revenue? OF COURSE!

2 thoughts on “How to Display Warning for Outdated Articles in WordPress”

  1. thanks again, works perfectly, Modified a bit as follows to check for posted date also

    function wpp_outdated_post_warning($content) {
    // Check if it’s a singular post
    if ( is_singular(‘post’) ) {
    $post_date = get_the_time(‘Y-m-d’);
    $modified_date = get_the_modified_time(‘Y-m-d’);
    $current_date = current_time(‘Y-m-d’);
    $outdated_threshold = strtotime(‘-2 years’); // Define outdated article age

    // Check if post is older than the threshold and hasn’t been modified
    if ( strtotime($post_date) < $outdated_threshold && $post_date === $modified_date ) {
    // Add warning message for posts not modified since publication
    $warning_message = 'Warning: This article was published on ‘ . get_the_time( ‘M j, Y’ ) . ‘. Some information may be outdated.’;
    $content = $warning_message . $content;
    }
    // Check if post has been modified but is still older than the threshold
    elseif ( strtotime($modified_date) < $outdated_threshold && $post_date !== $modified_date ) {
    // Add warning message for modified posts
    $warning_message = 'Warning: This article was last modified on ‘ . get_the_modified_time( ‘M j, Y’ ) . ‘. Some information may still be outdated.’;
    $content = $warning_message . $content;
    }
    }
    return $content;
    }
    add_filter( ‘the_content’, ‘wpp_outdated_post_warning’ );

    changed the CSS to
    .wpp-outdated-warning {
    background-color: #ffcc80; /* Light orange */
    color: #333; /* Darker text color for better contrast */
    padding: 10px; /* Increased padding for better spacing */
    border-radius: 8px; /* Slightly rounded corners */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle box shadow for depth */
    }

    Reply
    • You’re welcome! I’m glad it works well for you. Checking the posted date is a great improvement. It allows for a more precise warning message depending on the post’s history. Thank you for the code update!

      Reply

Leave a Comment

Share This
Hey 👋🏻
Do you have a WP blog?
If so, you may need these. All the resources you need to grow your blog.

Your popup content goes here

50%

Where should we send the template?

After entering your email address, you will be added to our newsletter subscribers. You can unsubscribe anytime.

Want to Build Passive Income Like the One on the Screenshot Below?

Click the button on the right side to learn how 👉🏻
5 easy steps to turn your WordPress knowledge into monthly recurring revenue.

Click the above button to close the popup.