Speed Project: Time Since Last Post
Speed Projects are a wonderful concept, that I stumbled across through FAT (Free Art & Technology). I've been thinking about writing a WordPress plugin for a while to motivate myself and to show site visitors, that my blogs are active.
Turns out, it only took me 25 minutes to produce a really tiny solution, that displays a human readable time since the last blog post was published. The credit is not really with me, but with the great documentation and pre-built WordPress functions.
It was amazingly easy to build a quick prototype, that I could put live right away. I'll spend some more time on it and make it more configurable, because I also want to use it on my German blog. Here's the code so far:
<?php
/*
Plugin Name: Time Since Last Post
Plugin URI: @TODO:
Description: Displays time passed since the last post was published
Author: Jonathan M. Hethey
Version: 0.1
Author URI: http://jonathanmh.com
*/
global $wp_version;
if((float)$wp_version >= 2.8){
class LastPost extends WP_Widget {
function LastPost() {
parent::WP_Widget(
'LastPost',
'Time Since Last Post Widget',
array(
'description' => 'Display the time passed since the last published post in a widget area'
)
);
}
function get_last() {
$options = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish'
);
$lastPost = wp_get_recent_posts($options);
return $lastPost[0];
}
function widget($args, $instance){
extract($args, EXTR_SKIP);
echo $before_widget;
$lastPost = $this->get_last();
$lastPostUnix = strtotime($lastPost['post_date_gmt'].' GMT');
$diff = human_time_diff( $lastPostUnix, current_time('timestamp') );
echo 'last post: ' . $diff . ' ago';
echo $after_widget;
}
}
add_action('widgets_init', 'lastPostInit');
function lastPostInit(){
register_widget('LastPost');
}
} // end of compatability check
?>