Sunday, June 20, 2010

Wordpress widget: Count all

About

I've been playing around with wordpress 3 to replace blogger.com, which was getting worse and worse over the years. Wordpress turned out to be quite nice, I was positively surprised to see what a great thing it has evolved to.

It has extensive widget support as well. But when I was looking for a baby age counter, I found 2, neither one of which worked. I think it was caused by their age; I've found a reference to wordpress 2.3, and the code was also looking quite horribly written. I'm not a php developer myself, but having extensive experience with other platforms, I can cope with it, and just by looking at it I could tell it was Bad Code (tm).

So I decided to write one. PHP didn't seem too hard. I've started off by googling for "wordpress widget tutorial", and found this great tutorial by Jesse Altman. It was basically a skeleton HelloWorld widget, well-written, well-explained. Without much further ado (except the PHP reference doc), I was able to create a baby counter widget.

Install

It's a single .php file; just drop it into the wp-content/plugins directory of your wordpress (or use the admin interface to install it directly).

Use

After activating, you can use the "Count all" widget under the 'Appearance' menu.



The values are self-explaining, but for reference:

  • Title is the widget's title. It can contain HTML; its value is simply embedded into the resulting HTML page.
  • Content is the widget content field. It uses the DateInterval::format function to format the field contents. You can use any field specifiers from that function. Its contents are also embedded into the resulting HTML page, so over the %-tags, you are free to use any HTML you want.
  • Date specifies the date we should count distance from. Since this plugin simply takes the difference between 'now' and the specified date, it can be used for counting down (for dates in the future), or counting up (for dates in the past). It's all up to you.
That's it. You can have multiple instances of this widget, of course.

Requirements

This plugin needs Wordpress 2.8+ (tested to work on 2.8.4 and 3.0), and PHP 5.3 (for the DateInterval class).

Download

Here


 

5 comments:

Dan said...

Installed and I'm getting the following error:
Fatal error: Call to undefined method DateTime::diff() in /home/litl3drg/public_html/wp/wp-content/plugins/count-all/count_all.php on line 57

WP Version 3.0.1
Settings:
Title: Hello
Content: %D Days %H Hours %I Minutes %S Seconds
Date: 2010-11-08 16:00
Thoughts?

Unknown said...

Same here. Too bad, cause the baby age counter thingie was exactly what i was looking for!

Ibi & Agoston said...

Dan, Yvonne:
Please check the 'requirements' section. PHP 5.3 is minimum for the dateinterval class.

danielseavey said...
This comment has been removed by the author.
danielseavey said...

Hello, I'm interested in using your wordpress widget. When I activate it however, it does not run.

WP Version
Setting:
Title: Countdown to Iditarod
Content: %m Months, %d Days, %h Hours, %i Minutes
Date: 2011-03-05 08:00

I would like to build a countdown with Months, Days, Hours and Minutes. I am not familiar with PHP. Can you perhaps provide what I need in the countdown field?

Thank you