Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. WordPress

How to Build a WordPress Plugin to Identify Old Posts


While reading articles on websites, I've found that when a post is over four years old, a notification stating that the post is old is displayed on certain websites. A similar notification is also found in WordPress plugin directory when a plugin hasn't been updated in over two years.

In this article, we will be building a plugin that displays a similar notification when a post is over X-years old where "X" an integer that denotes the number of years that defines old.
The plugin will provide you the option to specify your own custom notification sentence and the number of years before a post is considered old.

This plugin will be built using object-oriented programming in an attempt to make the code more organized and to adhere to the DRY principle. As such, some experience with object-oriented programming is required if you are to understand this tutorial.

Planning the Plugin

The plugin will consist of two protected class properties and ten methods . The properties are described below.

  • $_notification stores the notification message retrieved from the database that was previously set by the plugin user.
  • $_years stores the number of years retrieved from the database.

I will be explaining the role of each method (known as functions in procedural programming) and their respective code as we journey.

Coding the Plugin

First, let's include the plugin header, create the class and define the properties:

When writing WordPress plugins in OOP, all the action and filter hooks can be set in the constructor (which is named __construct). Our plugin's constructor method will consist of five functions: three action hooks, a filter hook and a register_activation_hook function.

  • The register_activation_hook calls the method to set the plugin default settings on activation.
  • The next three add_action functions call the hook functions to register the plugin menu, hook the plugin section and field to admin_init and add the plugin stylesheet to header respectively.
  • The add_filter call the displayNotification method which displays the notification when a post is old.

Looking at the __construct method above, the register_activation_hook function calls the aop_settings_default_values method to set the default plugin settings.

The aop_settings_menu method creates the plugin sub-menu under the existing Settings menu.

The third argument passed to the add_options_page function above is the alert_post_old_function method that displays the page content of the plugin's settings.

To add the plugin settings, we will be using WordPress Settings API to add the settings forms.

Firstly, we define the section, add the settings fields and finally register the settings. All this will be done in the pluginOption method that was hooked to the admin_init action earlier in the __construct method.

The settings field callback method: aop_notification and aop_years that fill fills the field with the desired form inputs are as follows.

We will retrieve the plugin notification and year settings and store them in the two protected properties: $_notification and $_years because they will come in handy when we are to determine if a post is over the set age and when displaying the notification message.

The CSS code use in styling the notification will be in the stylesheet method.

Finally, the function that displays the notification above the post content that was deemed old is as follows:

Let's talk through the code above: First, we retrieve the number of years that determine when a post is old, subtracted the year the post was written from the present year. If the result is greater than the year that defines old, the notification that the post is old is displayed.

Finally, we are done coding the plugin class. To put the class to work, we need to instantiate it like so:


In this article, we learned how to calculate the age of a post, display a notification when a post is considered old and did so using object-oriented programming practices.

I urge you to review the code to gain an in-depth knowledge of how it works. Your questions, comments, and contributions are welcome.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.