Archive for February, 2010


I’ve made my first handful of widget plugins using wordpress.  The first one took me a little over an hour.  The others went by after about twenty minutes.  Once you get the hang of the first one the pattern sets in and the process gets pretty easy.  That isn’t to say that there aren’t some amazingly complex plugins, but the general develop is simple enough to justify using plugins for much smaller tasks.

In my situation, I’m supporting thirty odd websites that all use similar layouts, but they all have a different look and feel.  The individual site owners customarily have a strong say it what goes on their site and how it is to be displayed.  Because these sites are all separate and distinct and may need to be developed independently, I’ve chosen not to unify them as pages of a larger site.  Instead, each site is a separate wordpress.  At current, each site uses the same wordpress theme (developed by me), but that may change.

In this situation, widgets have been invaluable.  They let the individual site owners customize their website but moving the widgets up and down or adding/removing them entirely.  While this arrangement has been amazingly beneficial a few problems have arisen with some of the standard content items between pages.  Every site, for example, has a link to a search tool that exists on a different site.  The obvious solution is to create a text widget, drop the search html code in, and call it a day.  This works…but it requires the site owners, (non-technical) to drop this code in and not drop anything else that might cause problems…that itself is a problem.

I knew early on that an adequate solution would be to “templatize” these common widgets.  For example, I created the search widget with the proper html already included.  I’ve dropped this template into each of the site directories and activated them, so all the directors have to do is move the widget around as necessary.  I’ve done something similar with an ‘hours of operation’ widget, except I’ve given the owners a web form where they can input their actual hours.  The public facing formatting is taken care of by the widget itself and the theme specific stylesheets.

Overall, I was pleasantly surprised to find out that widget development is simple enough that using it as a solution to easy but non-trivial issues is worth the time and effort.

As an example, I’ll break down my hours of operation code:

<?php
/*
Plugin Name: Library Hours
Description: Widget with Control Panel
Author: Robert Drake
Version: 1.0
Date: 1/21/2010
Author URI: RobertDrake.net
*/

This is just a stock comment block to describe the code below, but the name and description are used in wordpress’ backend plugin management so make them useful.

class MHLS_LibraryHours_Widget extends WP_Widget {

By surrounding the rest of the code in this wrapped you take advantage of a whole bunch of code built into wordpress 2.8 that does a lot of the widget stuff for you.  Older guides tend to include this code and the systems are backwards compatible (but not forward, meaning my code wont work prior to 2.8).

function MHLS_LibraryHours_Widget() {
$widget_ops = array('classname' => 'widget_mhls_libraryhours', 'description' => 'Library Hours' );
$this->WP_Widget('mhls_librarywidget', 'MHLS_Library_Hours', $widget_ops);
}

This block is honestly pretty confusing and as far as I can tell most of the names dont really matter all that much as long as you match the function name with the init at the end.  Otherwise you specify a few look and feel type of things in the widget control panel and setup what prefix will go with your variables.  Luckily, all that stuff is handled for you.

function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? '&nbsp;' : apply_filters('widget_title', $instance['title']);
$monday = empty($instance['monday']) ? '&nbsp;' : apply_filters('widget_monday', $instance['monday']);
$tuesday = empty($instance['tuesday']) ? '&nbsp;' : apply_filters('widget_tuesday', $instance['tuesday']);
$wednesday = empty($instance['wednesday']) ? '&nbsp;' : apply_filters('widget_wednesday', $instance['wednesday']);
$thursday = empty($instance['thursday']) ? '&nbsp;' : apply_filters('widget_thursday', $instance['thursday']);
$friday = empty($instance['friday']) ? '&nbsp;' : apply_filters('widget_friday', $instance['friday']);
$saturday = empty($instance['saturday']) ? '&nbsp;' : apply_filters('widget_saturday', $instance['saturday']);
$sunday = empty($instance['sunday']) ? '&nbsp;' : apply_filters('widget_sunday', $instance['sunday']);
echo $before_title . $title . $after_title;
echo '<div id="libraryhourswidget">';
echo 'Monday: <span>' . $monday . '</span><br>';
echo 'Tuesday: <span>' . $tuesday . '</span><br>';
echo 'Wednesday: <span>' . $wednesday . '</span><br>';
echo 'Thursday: <span>' . $thursday . '</span><br>';
echo 'Friday: <span>' . $friday . '</span><br>';
echo 'Saturday: <span>' . $saturday . '</span><br>';
echo 'Sunday: <span>' . $sunday . '</span><br>';
echo '</div>';
echo $after_widget;
}

This is really the meat of the plugin.  Extract takes the data (which is set elsewhere), assigns it to variables, and displays in straight html/css.  This is what will ultimately be output to the screen so whatever you want to see needs to go here.  Throughout the code you’ll notice a $instance being used frequently.  Basically, when the variable names you use aren’t the full variable name.  When I work with the variable monday, I’m actually working with the variable ‘widget_mhls_libraryhours 3 monday’  The first part of the name is specific to the widget name, and the second part is specific to the widget instance.  In order to have multiple copies of the same widget you need to use the instance name to separate one copy from another.

The rest of that function is fairly boring.  The $monday = part uses the tertiary operator in php to assign the variable to a non breaking space if there is no data in the variable.  In another plugin, I had it assign a 0 so I could test for false later and restrict my output.  Otherwise the filters are just sanitizing the user inputs for anything that looks like code.

function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['monday'] = strip_tags($new_instance['monday']);
$instance['tuesday'] = strip_tags($new_instance['tuesday']);
$instance['wednesday'] = strip_tags($new_instance['wednesday']);
$instance['thursday'] = strip_tags($new_instance['thursday']);
$instance['friday'] = strip_tags($new_instance['friday']);
$instance['saturday'] = strip_tags($new_instance['saturday']);
$instance['sunday'] = strip_tags($new_instance['sunday']);
return $instance;
}

Easy function.  When the user changes the data in the form fields this function updates the variables with the new values.  That’s it.  Cake!

function form($instance) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'entry_title' => '', 'comments_title' => '' ) );
$title = strip_tags($instance['title']);
$monday = strip_tags($instance['monday']);
$tuesday = strip_tags($instance['tuesday']);
$wednesday = strip_tags($instance['wednesday']);
$thursday = strip_tags($instance['thursday']);
$friday = strip_tags($instance['friday']);
$saturday = strip_tags($instance['saturday']);
$sunday = strip_tags($instance['sunday']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('monday'); ?>">Monday: <input id="<?php echo $this->get_field_id('monday'); ?>" name="<?php echo $this->get_field_name('monday'); ?>" type="text" value="<?php echo attribute_escape($monday); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('tuesday'); ?>">Tuesday: <input id="<?php echo $this->get_field_id('tuesday'); ?>" name="<?php echo $this->get_field_name('tuesday'); ?>" type="text" value="<?php echo attribute_escape($tuesday); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('wednesday'); ?>">Wednesday: <input id="<?php echo $this->get_field_id('wednesday'); ?>" name="<?php echo $this->get_field_name('wednesday'); ?>" type="text" value="<?php echo attribute_escape($wednesday); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('thursday'); ?>">Thursday: <input id="<?php echo $this->get_field_id('thursday'); ?>" name="<?php echo $this->get_field_name('thursday'); ?>" type="text" value="<?php echo attribute_escape($thursday); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('friday'); ?>">Friday: <input id="<?php echo $this->get_field_id('friday'); ?>" name="<?php echo $this->get_field_name('friday'); ?>" type="text" value="<?php echo attribute_escape($friday); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('saturday'); ?>">Saturday: <input id="<?php echo $this->get_field_id('saturday'); ?>" name="<?php echo $this->get_field_name('saturday'); ?>" type="text" value="<?php echo attribute_escape($saturday); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('sunday'); ?>">Sunday: <input id="<?php echo $this->get_field_id('sunday'); ?>" name="<?php echo $this->get_field_name('sunday'); ?>" type="text" value="<?php echo attribute_escape($sunday); ?>" /></label></p>
<?php
}
}

This function (and I’ve included the closing bracket of the starting class) looks kind of obnoxious but its really just setting up the user form in the backend.  The top half sets the variables and the second half sets a form.  You may be wondering why there is no <form> in there.  WordPress actually takes care of that stuff so no worries.  It’ll drop your code into its own form and use the standard formatting of the admin panel so everything looks uniform.

add_action( 'widgets_init', create_function('', 'return register_widget("MHLS_LibraryHours_Widget");') );

This is the last line.  It’s used when activating the plugin and registers everything in the appropriate places for wordpress to work with.  Simple plugin.  Simple code.

That’s it.  I’m looking forward to coding up some more and more interesting plugins.  I’ve already got a few ideas in mind for things that would help me.  I’ll try to post things periodically as they become interesting.

First, an article on the Fourth Amendment.

As a network administrator for a library system and a technical advisor for a cloud computer company, I have a certain vague relationship with electronic law.  More often, technical necessity and security best practices determine how things should be done, but beyond all this there are legal requirements that do have to be met and further legal protections that should be protected.

The key paragraph in that link is:

In order for enough trust to be built into the online cloud economy, however, governments should endeavor to build a legal framework that respects corporate and individual privacy, and overall data security. While national security is important, governments must be careful not to create an atmosphere in which the customers and vendors of the cloud distrust their ability to securely conduct business within the jurisdiction, either directly or indirectly.

Laws must be appraised on a few points.  Most importantly, how and why does a law infringe upon the rights of its citizens.  Do the law’s protections provide an adequate compensation for the freedoms infringed?   Finally, does the law provide a mechanism of recourse for those parties that have been affected.

As I see it, any law regarding cloud computing needs to make data within the cloud fundamentally private, or private by default with a handful of acceptable cases for disclosure.  First, any non-intentional, non-targeted access by network administrators need to fall under a sort of ‘technician’s freedom’.   If an account is having problems and a technician logs in to figure out the problem and happens to see some emails, this should not count as a privacy breach, assuming that this was a reasonable response to the problem and that reasonable precautions were taken to prevent that sort of disclosure.  As usual, any sort of warrent of subpoena should be honored, but the standards of access need to be held high otherwise the integrity of the cloud are lost.

The idea that data must be encrypted to be considered secure seems faulty.  Certainly data that is put up for public or open access is not ‘protected’.  An open facebook page has no assumption of privacy, but an uploaded google doc does and should, regardless of encryption.  Forcing encryption might be good technically (and probably is), but it does not serve any particular useful legal standard.  Rather, saying that only encrypted documents are private within the cloud, suggests that the cloud, regardless of any user access that a customer would logically or contractual expect, has no protections whatsoever.  A different analogy than the ‘locked briefcase’ should be considered.

‘I smelled something’ type police work should not be encouraged on the internet.  Crime inevitably leaves a trial, usually a financial trail, and that’s where police work needs to start and end whether it be common fraud or the terrorism that will inevitable be used to justify some overarching data-mining operation that won’t actually help anyone.  Any terrorist dumb enough to put any easily found keywords in his online documentation probably would have gotten caught anyone, and anyone more clever than that is going to be lost within the the billions of terabytes of data online.  That’s another argument altogether, but as far as fledging cloud computer is concerned, some decent privacy laws are an absolute requirement both to reassure the customer and to bind the providers into a respect for privacy and security that, thus far, has been noticeably lax, as the frequent data breaches suggest.

I will grudgingly admit that today is holiday, but if I’m going to celebrate some unnecessary Saint’s day I’d prefer to do St. Patties…again, but before.  This other holiday people celebrate is a damn stupid thing, but comes with an interesting roman background.   Romantic stuff, eh?  Where can I buy a Valentine’s Day card with a picture of the Lupercal Cave on it…  Guess I’ll have to stick to my usual 2/14 decoration.

Beautiful Night Shots

I must have played Sim City 2000 for five years straight.  I made a thousand cities, some small farmer villages other suburban burgs lined with trees and schools.  I made epic metropolises that covered the ground like moss and rose into the sky, well beyond the long lost streets.  The later simcity games were even more visceral.  Simcity 4 was practically a city planning guide and my cities, all interconnected and interdependent, were amorphous and uncontrollable.  They ate away at the land and leveled entire mountains, expanding without any particular guide or reason.  Some cities I let flounder, others I forced higher and wider, hungry organisms intent on becoming organisms of their own.

While the later games, simcity 3000 and simcity 4, were visually stunning and phenomenal games, they lacked part of what made simcity 2000 special and not the least of that was the extended manual that came with the special edition (or perhaps commemorative edition, I don’t remember anymore.)  It was a standard game manual (back when games actually came with those.  Allow me a moment to weep over my long lost Baldur’s Gate manual, a dusty tome with faded pages and gold ink.  That was a masterpiece.)  The simcity 2000 manual came with a dozen essays and a similar number of images written about cities.  They described cities as homes of people and their lives and similarly as mindless corporate jails where the inhabitants are little more than guests.  They applauded the glorious gentrification of a decaying, forgotten neighborhood and bemoaned the crime infested grime of the city center.

Simple as that manual was, those writers are really what gave me an interest in ‘the city’ as something more than a crowded nuisance, a place to live while hoping to live elsewhere.  The photos found at .  A ghostly Big Ben, the Eiffel Tower looming over a darkened city, bright streets dizzying with activity, all cities as lived in and inevitably forgotten as Rome.  It is history, like a textbook, but it is current history and so it is very alien and tells a story without ignoring the details.

Evil Crime Syndicate

Does this mean I’m getting some article reviewers and wordpress designers on staff soon?

When…did this happen?  I knew about the handful of recent Lego computer games, which have gone a loooong way since the old (and pretty terrible) lego computer game I had, but an MMO?  That could be…interesting and the trailer is amazing.

I took this recipe and modified it.  I couldn’t find any sliced Gouda (and the gouda for sale was in triangular wedges) so I got Munster instead.  Munster is usually sliced thicker so I only used 1 or 2 slices per roll.  I also didn’t have a panini maker, but a simple pan fry with plenty of butter worked out just fine.  I thought the mustard + honey combination might be have turned out badly, but it’s excellent.  For the future I’d probably make more honey and do the insides of both halves.  Also, I found the roasted tomato a bit distracting so I pulled that off the sandwich and at it later.  The whole thing would go well with some kettle chips or potato salad.  Excellent recipe.

(Also thanks for the Christmas gift cookbook.  That’s where I originally found the recipe.  It was used in Top Chef a few seasons ago)

Someone sent me these:  mash-ups of Star Wars and 80s Television Show Intros.

That’s the picture that hangs above my laptop.  I stare into it as a I write.  I am not quite sure what made me decide to write about it, but I suppose it’s only natural.  I’ve had it for maybe six months now.  Most people know of Gérôme (if they know of Gérôme) from his depiction of gladiators, which is most famous for the thumbs down symbol.

I actually came to learn of Gérôme from some random bit of historical reading I was doing at the time.  Gérôme did a depiction of the Grand Conde, who was a famous French general during the Thirty-Years war (and one of the great generals in history.)

Sometime later I searched out the artist and found Gerome.  His historical works are my favorite, but history seems to have preferred his orientalist undertakings.  In researching this article I found this gallery, which is rather extensive.   Or this one, which has everything (but with more annoying pagination).  Enjoy!