Search

How to Display Warning for Outdated Articles in Divi

Adding warnings to outdated articles can greatly benefit users, ensuring they approach older content with caution and consider seeking more up-to-date information. If you’re utilizing Divi to build your website, this tutorial will guide you through the process of adding warnings to your outdated articles!

Previously, we covered how to add warnings to outdated articles in WordPress, which worked seamlessly with default WordPress setups and page builders like Elementor. However, in Divi, using the code with the the_content hook posed an issue, as it prevented the visual builder from opening on our end.

Fortunately, within Divi, we have the option to utilize the Theme Builder to create a single post layout. This allows for easy application of customizations across all posts. Additionally, WordPress’s shortcode feature simplifies complex codes into easy-to-use text enclosed in square brackets, making it effortless to display warnings for outdated articles throughout your website.”

Displaying Warning for Outdated Article in Divi

Step 1: Add the Shortcode Code

To enable the [warning] shortcode on your website, the first step involves integrating a code. In this tutorial, we’ll focus on the most straightforward method: adding the code to the functions.php file of your currently active theme.

However, before proceeding, it’s advisable to create a backup of your WordPress files. You can accomplish this through backup plugins or by utilizing the built-in backup feature provided by your hosting service. Alternatively, consider safer methods like using a child theme or creating a custom plugin.

Once you’re prepared, navigate to AppearanceTheme File Editor in your WordPress dashboard. From there, select the functions.php file from the list of theme files. Copy the provided code to the bottom of the file, then click the Update File button to apply the changes.

function wpp_outdated_post() {
  
  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('-30 days'); // 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;
      }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;
      }        
    }
  }
 add_shortcode( 'warning', 'wpp_outdated_post' );

The provided code will issue a warning message stating, ‘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 articles published more than two years ago but have not been updated since.

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.

Step 2: Add the Shortcode to the Single Post Layout

The next step involves adding the shortcode to the single post layout. If you haven’t already set up a single post layout, you can refer to our previous article, which provides detailed instructions on creating one.

Once your layout is prepared, simply add a text module before the content and insert [warning] into its body.”

Utilizing a text module offers the flexibility to customize its style according to your preferences. For example, you can enhance the visual appeal of the warning message by incorporating a gradient background and slightly rounded corners, as illustrated below.

The Bottom Line

In conclusion, by following this tutorial, you’ll be able to implement warnings for outdated articles seamlessly within Divi, ensuring users approach older content with awareness and facilitating their access to more up-to-date information.

With Divi’s Theme Builder and shortcode capabilities, the process becomes straightforward, enabling you to enhance the user experience across your entire website effortlessly.

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 save yearly expense up to $219? why not?

Leave a Comment

Save your Divi assets to the cloud and access them from anywhere.