Migrating WordPress Plugins to Drupal Modules - Earth Hour

By:

on

March 22, 2009

It was Friday afternoon and went over to search for terms on Twitter that I like to keep an eye on when I ran into a post from @haggaret. It was a call to convert an Earth Hour WordPress Plugin over to Drupal.  Now I'm very aware of environmental issues, but there are 8765 (or so) other hours in the year to worry about, so what's the big deal about this one.  I'm still not sure, but in solidarity I decided to check it out and see when it was, unfortunately it was 8 days later on the last Saturday of March. 

But really, how hard could it be?  A bit of CSS, tad of JavaScript, and heck, most of the logic had already been figured out for WordPress.  It was a bit more difficult than it seemed at first, but never-the-less, by the time there was one week remaining until the Earth Hour countdown of 8:30-9:30pm on March 28th, 2009, I had a working module for Drupal. 

Was interesting to see how this spread through the Twitosphere, getting a few folks trying out the code within a few hours of providing a download able version.  I had a less functional version the previous evening that I posted to pastbin, but didn't get any feedback on.  I had thought it would be neat to get some spontaneous collaboration around improving this module if the right people were watching.

I don't have any prior experience with WordPress conversions, but it was interesting to get a look at the code and compare.  WordPress uses the Snoopy library and I couldn't find an easy equivalent in Drupal, so just pulled off a parsing tool from php.net.  There must be an equivalent library to pull from in Drupal, but didn't find it.

So much of these systems is learning the shortcuts that have been implemented.  I'm still not sure what '__' is supposed to mean in code like:

echo __( "Visit the Earth Hour Website", "earth-hour" );

It's interesting too also understand how when you are emulating someone else's work that it is easy to just adopt their assumptions. I'm still not sure why the WordPress Plugin sent site names over to the server in md5(). Certainly there are places where you would want that security, but if you are making a public statement, not sure it's a useful security measure.

Having dug into this over the weekend, I'm much more likely to attempt to migrate some other good WordPress Plugins.  There is certainly a lot to be learned from this type of review.

I will be extending the module and looking to generalize this type of module for other campaigns.  Since I was a bit late to get working on it (and there are only 5 days left before Earth Hour), I put in some code to ensure that I was able to pull up the right day every year:

$year = date('Y'); 
$last_day_of_march = date('D', strtotime(date("$year-3-31")));
$days_array = array('Mon' => -2, 'Tue' => -3, 'Wed' => -4, 'Thu' => -5, 'Fri' =>; -6, 'Sat' => 0, 'Sun' => -1); 
$last_sat_of_march = (31 + $days_array[$last_day_of_march]); 
$earth_hour_start_time = gmmktime(20, 30, 0, 3, $last_sat_of_march, $year);

In anycase, if you've got ideas for campaign modules like this, please let me know. It's all about raising awareness and changing behaviours after all. And if you've got a Drupal site, please consider installing the Earth Hour Module.

About The Author

Mike Gifford is the founder of OpenConcept Consulting Inc, which he started in 1999. Since then, he has been particularly active in developing and extending open source content management systems to allow people to get closer to their content. Before starting OpenConcept, Mike had worked for a number of national NGOs including Oxfam Canada and Friends of the Earth.