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