elektro:elch

Es ist nicht ungefährlich, wenn ein Volk lesen und schreiben kann.

Hello Dolly 2010

Since 2 weeks there’s a voting on Digging into WordPress whether to keep the Hello Dolly plugin in the standard distribution or to chuck it out the house.

One of the most heard reasons for the existence of Hello Dolly that it should be used as a pattern for novice plugin developers. In my mind the problem with this argumentation is that the plugin hasn’t changed essentially since WordPress v1.21 (the version which introduced plugins in May 2004).

Since then the environment for plugins has changed a lot. The main difference is that there are typically ten or more plugins installed on a blog. Therefore it’s mor important than ever that the plugins don’t interfere with each other. This can most easily be achieved by encapsulation and this leads inevitable to object-oriented programming. Here’s my attempt to take Hello Dolly to the next decade:

< ?php
/**
 * @package Hello_Dolly
 * @author Matt Mullenweg
 * @version 1.5.1
 */
/*
Plugin Name: Hello Dolly
Plugin URI: http://wordpress.org/#
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from Hello, Dolly in the upper right of your admin screen on every page.
Author: Matt Mullenweg
Version: 1.5.1
Author URI: http://ma.tt/
*/

if (! class_exists('Hello Dolly')) {
	class HelloDolly { 

		function hello_dolly_get_lyric() {
			/** These are the lyrics to Hello Dolly */
			$lyrics = "Hello, Dolly
			Well, hello, Dolly
                        [...]
			It's so nice to have you back where you belong
			You're lookin' swell, Dolly

			Dolly'll never go away
			Dolly'll never go away again";

			// Here we split it into lines
			$lyrics = explode("n", $lyrics);

			// And then randomly choose a line
			return wptexturize( $lyrics[ mt_rand(0, count($lyrics) - 1) ] );
		}

		// This just echoes the chosen line, we'll position it later
		function hello_dolly() {
			$chosen = $this->hello_dolly_get_lyric();
			echo "

$chosen

"; } // We need some CSS to position the paragraph function dolly_css() { // This makes sure that the posinioning is also good for right-to-left languages $x = ( 'rtl' == get_bloginfo( 'text_direction' ) ) ? 'left' : 'right'; echo " #dolly { position: absolute; top: 4.5em; margin: 0; padding: 0; $x: 215px; font-size: 11px; } "; } //dolly_css() } //class }//if class_exists() $HelloDolly = new HelloDolly; if (isset($HelloDolly)) { add_action('admin_head', array(&$HelloDolly, 'dolly_css')); // Now we set that function up to execute when the admin_footer action is called add_action('admin_footer', array(&$HelloDolly, 'hello_dolly')); } ?>

As you can see all the code is encapsuled in the HelloDolly class and the only variable names visible to the rest of the system are class HelloDolly and $HelloDolly. It’s not very likely that there will be another plugin using the variables.
This may not be too important in this small plugin but it gives the user a good and up-to-date prototype for more complex code. Maybe this code will convince the 76% that this new Dolly is a cow that is not only sacred but also meaty.


Comments

One response to “Hello Dolly 2010”

  1. Funny…while teaching myself plugin writing, I myself just converted Hello Dolly to object-oriented style. Then I Googled and found your page.

    I totally agree with you, the default installation should be a nice OO Dolly that people can learn from. Especially when PHP references are a little tricky to the beginner. We should push for this.

Leave a Reply

Your email address will not be published. Required fields are marked *