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

WordPress Multiple Tag Concatenation

Having used WordPress for over a year now I feel the time is right to share some tips I’ve found when creating pages, blog posts and mini-websites using WordPress . Some if not most are probably already being blogged but if not this may be of some use to you.

Combining Multiple WordPress Blog Tags for ‘Hybrid Pages’

I recently encountered the need to be able to segment my posts, to make them easier to categorise and hence link to allow visitors the ability to view relevant posts. Ever landed on a blog website and not known where to start when confronted by a long list of blog posts? Not very user friendly. Now if I create a post tagged ‘Customer Journey’ and another one called ‘Information Architecture’, someone visiting the website may want to filter out anything irrelevant aside from anything to do with ‘User Experience’ lets say.

There are a few options.

1. Let the user conduct a search (though we all know WordPress search is not the greatest…)

2. Add the tag ‘User Experience’ to your posts

3. Categorise your posts into ‘User Experience’

All 3 are viable options. However, with WordPress I discovered you can link to tags using

/tag/[TAG]

where the [TAG] is the single tag you wish to use; ensure you concatenate words using a hyphen e.g.

/tag/Customer-Experience

Will link to all posts tagged “Customer Experience”

However the smart guys at WordPress allow you to add 2 or more tags together e.g.

/tag/multi-channel+social-media/

which will search for all posts containing “multi channel” AND “Social Media”. Each tag is followed by the ‘+’ symbol. (currently nothing matches up!)

Finally if you wish to search for posts containing either keyword,

/tag/multi-channel,social-media/

hey presto, you’ve now searched all posts containing either tag using the comma as a separator. Clever? I think so…

The potential for this is three-fold

  1. Improved Usability – you can of course categorise your posts, but with ever changing post topics and varying tags you may have the need for ‘short term campaigns’ in terms of creating links to post categories to help users find information. Instead of changing categories you can simply choose which tags to point to. E.g. I’m blogging about holidays. As holidays are seasonal, I wish to now link to all beach holidays I’ve written about, in Europe that are ideal for families. Next month I wish to link to blogs related to winter holidays, in Europe ideal for families. Whilst categories would do the job (ensuring each and every post is categorised under ‘European Family Beach Holidays’ and ‘Winter European Family Holidays’ accordingly) it would be extremely time consuming should you wish to then also link to ‘Spanish Beach Holidays’ from your menu. Instead you can concatenate the tags and create links to all related posts.
  2. Search Engine Optimisation (SEO) – For SEO purposes, creating links is fantastic. You can use your keywords, which are called keywords as you expect this to be usable by your users. In the same way search engines will look upon your link text, link URL, title and body content quite favourably should you keywords match all the way down to content. Used as a menu link this signals to the search engine that you consider it important and so may your users….
  3. Blog Fluidity – Static menus are always a must for usability when it comes to designing a website. Blogs are far from static so you do at times need ways to link to the most relevant posts at a higher level and this is where concatenating tags is extremely useful.

I’m sure there are other ways (e.g. using the ?tag= parameter) however in building strong, usable URLs I’ve found search engines and users alike get on well with this method!

There are some other enhancements I’ve made on other WordPress installations, such as displaying the tag query ‘meaningfully’ and creating specific page layouts depending on tags used and I’ll aim to blog these at some point…

Depesh