Create a gist now

Instantly share code, notes, and snippets.

Check if a post is older than a certain date and, if so, returns a message.
<?php
/**
* Check if a post is older than a certain date and, if so, returns a message.
* The message can also be triggered under certain taxonomies.
* In addition posts that have certain taxonomies could ever be excluded.
* The message can be displayed only on single posts.
*
* @param array $args {
* The options for the function.
*
* @type string $time The time unit where to calculate the age of the post.
* Default 'year'.
* Accepts 'month', 'week', 'day', 'hour', 'minute'.
* @type integer $max_quantity The maximum value for the age of the post.
* @type string $taxonomy_include The name of the taxonomy under which the message is triggered.
* Default empty.
* Accepts any taxonomy name.
* @type string|int|array $term_include The term name/term_id/slug or array of them to check for.
* @type string $taxonomy_exclude The name of the taxonomy under which the message is not triggered.
* @type string|int|array $term_exclude The term name/term_id/slug or array of them to check for.
* @type bool $on_single_only Whether the function should be executed only on single posts.
* @type string $message The message to be displayed.
* The string must contain '%s' in order to display the number (1 year, 5 months, ecc.).
* }
*
* @example We want to display an alert message only
* - on technical posts that have the category 'Tech Ninja' and/or the category 'Computer Experts';
* - older than 6 months ago;
* - but we do not want the message on those posts that have the tag 'Featured' and/or the tag 'Premium Post'.
* - Also the message must be displayed on single posts only.
* In this case, here how to call the function:
* $args = array(
* 'time' => 'month',
* 'max_quantity' => 6,
* 'taxonomy_include' => 'category',
* 'term_include' => array( 'Tech Ninja', 'Computer Experts' ),
* 'taxonomy_exclude' => 'post_tag',
* 'term_exclude' => array( 'Featured', 'Premium post' ),
* 'on_single_only' => true,
* 'message' => 'This is a technical post of %s ago. Instructions could not be valid anymore.'
* );
* check_post_age( $args );
*
* @return string A HTML P element with a notice.
*/
function check_post_age( $args ) {
$defaults = array(
'time' => 'year',
'max_quantity' => 1,
'taxonomy_include' => '',
'term_include' => '',
'taxonomy_exclude' => '',
'term_exclude' => '',
'on_single_only' => true,
'message' => esc_html__( 'This post is obsolete! It was written %s ago.', 'my-translation-domain')
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
/*
* Check if on single posts we have to hide the message and make sure we are on a single post.
* Otherwise stop the function.
*/
if ( $on_single_only && ! is_single() )
return;
// Let's define $diff_time, the difference in seconds between today and the time of the post.
global $post;
$today = current_time( 'timestamp' );
$post_time = get_the_time( 'U', $post->ID );
$diff_time = $today - $post_time;
// Make sure $max_quantity is a non-negative integer.
$max_quantity = absint( $max_quantity );
// Lets define $limit_time, the time beyond which the message is triggered.
switch ( $time ) :
case 'year' :
$limit_time = YEAR_IN_SECONDS * $max_quantity;
break;
case 'month' :
$limit_time = 30 * DAY_IN_SECONDS * $max_quantity;
break;
case 'week' :
$limit_time = WEEK_IN_SECONDS * $max_quantity;
break;
case 'day' :
$limit_time = DAY_IN_SECONDS * $max_quantity;
break;
case 'hour' :
$limit_time = HOUR_IN_SECONDS * $max_quantity;
break;
case 'minute' :
$limit_time = MINUTE_IN_SECONDS * $max_quantity;
break;
endswitch;
$output = '';
// Check if the age of the post is beyond the limit time.
if ( $diff_time > $limit_time ) {
/*
* Check these conditions:
* 1. if $taxonomy_include is empty OR the post has the specified taxonomy
* 2. AND the post has not the specified taxonomy.
*
* The function empty( $taxonomy_include ) triggers the notice in case we do not specify any taxonomy,
* for example if we want that all posts older that the limit time should trigger the notice.
*/
if ( ( empty( $taxonomy_include ) || has_term( $term_include, $taxonomy_include, $post->ID ) ) && ! has_term( $term_exclude, $taxonomy_exclude, $post->ID ) ) {
$message = sprintf( $message, human_time_diff( $post_time, $today ) );
$output .= "\n" . '<!--googleoff: all-->' . "\n";
$output .= '<p class="obsolete-post-warning">' . $message . '</p>' . "\n";
$output .= '<!--googleon: all-->' . "\n\n";
}
}
return $output;
}
<?php
/**
* Call the function to check old posts and display an alert.
* The message is displayed under certain taxonomies,
* but not if the post has certain taxonomies.
*
* @uses check_post_age()
*/
function check_this_post_age( $content ) {
$args = array(
'time' => 'year',
'max_quantity' => 1,
'taxonomy_include' => 'category',
'term_include' => array( 'Tech Ninja', 'Wi-Fi Stereo' ),
'taxonomy_exclude' => 'post_tag',
'term_exclude' => array( 'Safe Tag', 'Save my post' ),
'on_single_only' => true,
'message' => 'This is a technical post I wrote %s ago. Be careful and test everything!',
);
$content = check_post_age( $args ) . $content;
return $content;
}
add_filter( 'the_content', 'check_this_post_age' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment