jump to navigation

Extra Tip: Object Oriented Plugin Development March 5, 2006

Posted by weeklytips in Uncategorized.
trackback

Note:As this was slightly chopped together from the book itself, it has some references to an earlier example project provided by the book. Do not fret, you should be able to figure out what’s happening, the example is pretty easy (this assumes you know the basics of writing a procedural plugin first).

To avoid function collision and pollution, many people have wanted to resort to building plugins with classes and methods instead of prefixed functions for their plugins. WordPress has the capability out of the box to support these sorts of plugins, but unfortunately the documentation around them is lacking.

Adapting Procedural Plugins

To show the differences between procedural and object oriented plugins, we’re going to first adapt the simple plugin from our first example to show the basic differences. If you don’t already know how to write in PHP style object oriented syntax, it may be in your best interest to read a bit of the php.net documentation (4, 5) on the subject first. Our original procedural plugin for profanity filtering looked like this:

function prefix_filter_profanity($content = ”) {
$profane_things = array(‘water’, ‘cold’, ‘fast’, ‘cheese’);
$clean_things = array(‘wine’, ‘hot’, ‘slow’, ‘pasta’);
$filtered_text = str_replace($profane_things, $clean_things, $unfiltered_text);
return $filtered_text;
}
add_filter(‘the_content’, ‘prefix_filter_profanity’);
add_filter(‘comment_text’, ‘prefix_filter_profanity’);

Adapting the plugin is incredibly simple.

class Profanity_Filter() {
/* This is for PHP5, which uses a new magical function as the constructor */
function __construct() {
$this->Profanity_Filter();
}
function Profanity_Filter() {
add_filter(‘the_content’, array(&$this, ‘filter’));
add_filter(‘comment_text’, array(&$this, ‘filter’));
}
function filter() {
$profane_things = array(‘water’, ‘cold’, ‘fast’, ‘cheese’);
$clean_things = array(‘wine’, ‘hot’, ‘slow’, ‘pasta’);
$filtered_text = str_replace($profane_things, $clean_things, $unfiltered_text);
return $filtered_text;
}
}
$profanity_filter = new Profanity_Filter;

The first look at this class may be a little overwhelming. The code has gotten considerably longer, but this is an exception rather than the rule, as longer plugins will often have their size cut down.

Let’s step through bit by bit.

function __construct() {
$this->Profanity_Filter();
}
function Profanity_Filter() {
add_filter(‘the_content’, array(&$this, ‘filter’));
add_filter(‘comment_text’, array(&$this, ‘filter’));
}

The first function, __construct, is for PHP5 and future versions. It basically ensures that the old style constructor, the method that matches the class’s name, is run when the class is created. The function Profanity_Filter is the old style constructor for PHP4. This function registers the actions for filtering on the content and comment text. This is similar to the old code, except the second argument, the callback function. Instead of passing in a function name as a string, you pass in an array, which corresponds to the PHP callback type. The callback type tells PHP how to find the function or method that its looking for. The first item in the array is a reference to $this, which is a reserved variable pointing to the current instance of the class. The second argument is the string name of the method inside the function that should be run as the filter, just like before.

This code can also be written in a second way, if your goal is solely to avoid name collisions.

class Profanity_Filter() {
function Profanity_Filter() {
add_filter(‘the_content’, array(&$this, ‘filter’));
add_filter(‘comment_text’, array(&$this, ‘filter’));
}
function filter() {
$profane_things = array(‘water’, ‘cold’, ‘fast’, ‘cheese’);
$clean_things = array(‘wine’, ‘hot’, ‘slow’, ‘pasta’);
$filtered_text = str_replace($profane_things, $clean_things, $unfiltered_text);
return $filtered_text;
}
}
/* ClassName::Method and array(‘ClassName’, ‘Method’) are both valid static callback types */
add_filter(‘the_content’, ‘Profanity_Filter::filter’);
add_filter(‘comment_text’, array(‘Profanity_Filter’, ‘filter’));

This method uses static calling of methods instead of requiring that you create an instance of a class. The best idea is to choose which method you use based on what your plugin needs to do. If you have code that would normally be written as object oriented code with multiple objects, the former method is the way for you, while the latter example is just for avoiding function name collisions.

Advertisements

Comments»

1. Extra Tip: Object Oriented Plugin Development - January 8, 2010

[…] Extra Tip: Object Oriented Plugin Development March 5, 2006 […]

2. Weekly Tip 2: Using Snoopy to Fetch Content « Blog Face2bouk - January 10, 2010

[…] Extra Tip: Object Oriented Plugin Development […]

3. Extra Tip: Removing Admin Menus « Blog Face2bouk - January 11, 2010

[…] Extra Tip: Object Oriented Plugin Development […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: