By Stephen Walker

Share

I wanted to use Avada conditional rendering function to show an information box if the content had not been updated in more than three years. Unfortunately, as of the date of this article, the published or modified dates are not one of the conditions you can choose. So I thought I would try ChatGPT.

Create a WordPress shortcode that only renders a shortcode named "outdated_alert" when the published date is 3 or more years old.

The concept here is to create a global element in Avada and then have it rendered on any post older than three years old. The reason for the global element is that I can modify the content as needed.

function check_outdated_content_shortcode($atts, $content = null) {
    global $post;

    // Get the post's published date and current date.
    $published_date = new DateTime(get_the_date('Y-m-d', $post->ID));
    $current_date = new DateTime();

    // Calculate the difference in years.
    $date_diff = $current_date->diff($published_date)->y;

    // Check if the published date is 3 or more years old.
    if ($date_diff >= 3) {
        // Render the "outdated_alert" shortcode.
        return do_shortcode('[outdated_alert]');
    }

    // If the published date is less than 3 years old, return an empty string.
    return '';
}
add_shortcode('check_outdated_content', 'check_outdated_content_shortcode');

Originally published on March 16, 2023

AP Style Date Shortcode
Table of Contents Border

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

RELATED Snippets

  • ChatGPT, PHP

    I prefer to have my dates in human-readable time difference or AP style dates, so this function will replace the default date format and output either the human-readable time difference or if the date difference is more than 180 days, the AP Style date.

  • ChatGPT, PHP, Shortcode

    WordPress has a great function built-in to express the date posted in a more readable format.

  • ChatGPT, PHP

    If you work with numerous non-published posts, it might be helpful to show the status. This shortcode shows the status if the status is currently not “Publish.”

  • ChatGPT, PHP, Shortcode

    I used ChatGPT to generate a shortcode that would indicate whether a business is opened or closed.

  • Advanced Custom Fields (ACF), PHP, Plugins

    Since ACF turns off the WordPress custom fields, this snippet will bring them back. Just add it to your functions.php or your favorite code manager

  • PHP, Shortcode

    I wanted to show the date the article was posted and the date updated if the dates differ. This shortcode will do just that.