WordPress Expired Post Notification


Having been working on a WordPress site for someone I’ve brushed up my PHP skills and enjoyed using a powerful blogging-cum-content management system, which to be honest provides a great deal of flexibility and control yet delivers intuitive ease of use.

So the latest problem was on how we could set a date for certain posts to automatically ‘expire’ in as far as having a notification appear within a tag/categories (archives) listings page or within the blog post itself.

The easy thing was to learn how to use ‘Custom Fields’ to set any data you wanted attached to the post. We were already adding dates, so that for example, if we required the article to expire at the end of June, we could simply enter 30/06/2009, and display this as a message on any page where the post was shown:

$expiry = get_post_meta($post->ID, “expiry”, true); //eg 31/03/2009 = 31st March 2009 (UK date format)

then in the HTML, enter:

<p>This post expires on <? echo $expiry; ?> </p>

So what happens when the post has actually expired? Your message would be a little out of date; ‘.. post expires on’ needs to become ‘.. post has expired’.

This seemed easy enough. Using PHP’s built-in function to convert any date string into a set of numbers which could be easily compared, I set about doing just that. However a few hours later I just could not get it working. The problem? I’m in the UK, and the function I was using, ‘strtotime’, expects the date string in US format!

UK format = dd/mm/yyyy

US format = mm/dd/yyyy

So, here is the final solution I came up, perhaps you’ll find a good use for it too!

1. Get the expiry date from your post Custom Fields (this has to be within ‘the loop’ in WP)
$expiry = get_post_meta($post->ID, “expiry”, true); //eg 31/03/2009 = 31st March 2009

2. To check for a valid date I’m only checking that the string has a forward slash
if(stristr($expiry, “/”) != “” ) {

3. Get today’s date as MM/DD/YYY
$todays_date = date(“m/d/Y”); //eg 05/06/2009 = 6th May 2009 – need this as ‘strtotime’ expects US date format

4. Convert this into a ‘time’ format, which makes comparison of absolute numbers easier (number of seconds since 1970…)
$today = strtotime($todays_date); //eg 123456789000

5. convert our UK date format from our blog post to US format by spliting the contents into an array then putting back together again in US date format
$expdate = explode(“/”,$expiry);
$expiration_date = date(“m/d/Y”,mktime(0,0,0,$expdate[1],$expdate[0],$expdate[2])).”\n”;

6. Convert this into a ‘time’ format, which makes comparison of absolute numbers easier
$expiration_date = strtotime($expiration_date); //eg 1234567890001

7. Now compare the 2 numbers! If your expiry date value is higher than today’s value (i.e. it is further from 1970 than today) then your blog post is still valid!

if ($expiration_date > $today) {
//place your code here for valid posts
} else
{
//place your code here for expired posts
}

If there are any better, streamlined ways of doing this please drop your thoughts!

Advertisements

4 thoughts on “WordPress Expired Post Notification

  1. that sounds good, have you got any example of this? in any webpage. It looks a bit too much for me but thats what i’m looking to do, where posts do not automatically delete after the expiration date, but they get a notification/warning that its is expired.

    thanks

  2. Damn! Too complicated for me..lol

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s