888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

January 5, 2008

In the first part of the series I looked at the three stages of plugin development: Classless, Static, and Object Based. In this part I will be demonstrating how you can use an additional class to separate common tasks into objects that can be reused, and so simplify the core plugin code.

For this example I am going to create a textbox as an external object. The plugin will require three textboxes on an admin page and will save the results of the input to a WordPress option.

Each textbox needs to retrieve its value, make that available to our plugin, output appropriate html to allow it to be changed, and if it is changed save it back to the database.

Without objects the plugin can look like this:

[php]
//note: I have omitted some value checking and validation for ease of reading
class my_plugin
{

//PHP 4 style constructor
function my_plugin(){

add_action( 'admin_menu' , array(&$this , 'add_admin_menu') );

}

function add_admin_menu(){

add_options_page('My Plugin Options', 'My Plugin Options', 9, basename(__FILE__), array(&$this, 'admin_page'));

}

function admin_page(){

$option1 = get_option('my_plugin_option_1');
$option2 = get_option('my_plugin_option_2');
$option3 = get_option('my_plugin_option_3');

if ( isset( $_POST['submit_my_plugin_options'] ) ) {

$option1 = $_POST['my_plugin_option_1'];
$option2 = $_POST['my_plugin_option_2'];
$option3 = $_POST['my_plugin_option_3'];

update_option('my_plugin_option_1', $option1);
update_option('my_plugin_option_2', $option2);
update_option('my_plugin_option_3', $option3);

}
?>

My Admin Page

}

}

$my_plugin = new my_plugin();

To make the object we need to move most of the contents of the admin page, into the textbox class. We'll start with the empty object, including the constructor:

[php]
class my_text_box{

//php 4 constructor
function my_text_box( ) {

}

}

So with an empty object, the first thing we need to do is to make sure that the option, and the form control will be unique. To do this we will create a prefix that will apply to any text box we create with this option and a name that will be set for each individual text box. At this point we'll also declare a variable that will hold the value.

[php]
class my_text_box
{

var $prefix = 'my_text_box_';
var $name = '';
var $value = '';

//php 4 constructor
function my_text_box( $name ) {

$this->name = $name;

}

}

Now we are in a position to interact with the database, via the WordPress options support, and with the user by outputing html.

We need to check to see if this textbox already has a value in the database, then check to see if the user is submitting a new value, and if so update that.

[php]
class my_text_box
{

var $prefix = 'my_text_box_';
var $name = '';
var $value = '';

//php 4 constructor
function my_text_box( $name ) {

$this->name = $name;
if ( $v = get_option( $this->prefix . $this->name ) ) {
$this->value = $v;
}

if ( isset( $_POST[$this->prefix . $this->name] ) ) {
$this->value = $_POST[$this->prefix . $this->name];
update_option( $this->prefix . $this->name , $this->value );
}
}
}

Finally we need to add a function to output the html to the page, so I have added another variable, a description, to the constructor that will be used for the label and a display function to output the html.

[php]
//note this example does not perform any validation.
class my_text_box
{

var $prefix = 'my_text_box_';
var $name = '';
var $value = '';
var $description = '';

//php 4 constructor
function my_text_box( $name , $description ) {

$this->name = $name;
$this->description = $description;

if ( $v = get_option( $this->prefix . $this->name ) ) {
$this->value = $v;
}

if ( isset( $_POST[$this->prefix . $this->name] ) ) {
$this->value = $_POST[$this->prefix . $this->name];
update_option( $this->prefix . $this->name , $this->value );
}
}

function display() {

echo '

';
echo '';
echo '
';
echo '

';

}
}

With this class as part of the plugin the textbox code will be identical for every text box and can be changed centrally. The final plugin will include the textbox class I have just created and the original plugin code we looked at, at the start of the post, can be reduced to this below:

[php]
class my_plugin
{

//PHP 4 style constructor
function my_plugin(){

add_action( 'admin_menu' , array(&$this , 'add_admin_menu') );

}

function add_admin_menu(){

add_options_page('My Plugin Options', 'My Plugin Options', 9, basename(__FILE__), array(&$this, 'admin_page'));

}

function admin_page(){

$option1 = new my_text_box( 'Option_1' , 'First Option');
$option2 = new my_text_box( 'Option_2' , 'Second Option');
$option3 = new my_text_box( 'Option_3' , 'Third Option');

?>

My Admin Page

display(); ?>

display(); ?>

display(); ?>

}

}

$my_plugin = new my_plugin();

You can download a sample plugin containing the finished code here.

It might not seem as though there is much of a saving in this example. If you think that you are right. In fact there are an extra 15-20 lines (including blanks) in the new version once the constructor has been added, but that is a result of the particularly simple example I have used.

Imagine though that your plugin has several admin pages and it starts to become a little clearer how much easier the actual plugin code itself will be.

There are also other occasions where objects are the natural choice.

My tabbed sidebars plugin allows the user to decide how many tabbed sidebars they want. However many they choose that is how many sidebars need to be registered, how many widgets need to be created and how many functions to tell the widgets what to display that need to be created. Using an object is the perfect way to handle this.

If you are considering whether to use objects I encourage you to take a look at the code for that plugin.

In this part I have demonstrated creating a separate object to perform routine tasks, such as creating textboxes and handling their submission; in part 3 I am going to look at how objects can inherit from other objects, making it easier to create complimentary objects, such as textareas, and textboxes.

Post Image The Easy Peasy Way
Wp postimages order?
26 comments
page 1065
Html 5 Gallery
Html 5 image gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Php post imagesmedium?
26 comments
page 1065
Fun With Sidebar Tabs Styling
Sidebar image interesting?
2 comments
page 336
Post Image The Easy Peasy Way
Postimagesmedium?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp codex postimagesmedium?
26 comments
page 1065
Fun With Sidebar Tabs Styling
Styling tabs css?
2 comments
page 336
Post Image The Easy Peasy Way
Theimage wordpress?
26 comments
page 1065
How To Add Sidebars To A Theme
Add widgetized footer in wordpress theme?
10 comments
page 1053
Html 5 Gallery
Html5 image gallery?
6 comments
page 1305
Wordpress Vs Graffiti
Graffiti yb combined?
8 comments
page 95
Poll Daddy Reviewed
Polldaddy poll?
2 comments
page 42
Html 5 Gallery
Html5 gallery?
6 comments
page 1305
Html 5 Gallery
Html5 gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Wp display all post images?
26 comments
page 1065
Why I Ditched Disqus
Add wordpress comments to friendfeed?
5 comments
page 1175
Using Your Own Url Shortener
Constructor and link shortner?
4 comments
page 1190
Quick N Dirty Post Exclusion
Fitler theloop addfilter?
11 comments
page 124
My Experience Of Flexx
Flexx of genesis wp theme?
4 comments
page 1026
Html 5 Gallery
Html5?
6 comments
page 1305
Wordpress Chat
Tchat page wordpress?
3 comments
page 1308
Quick N Dirty Post Exclusion
Filter wordpress posts plugin?
11 comments
page 124
Post Image The Easy Peasy Way
Wordpress attach images to post?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp post orderby?
26 comments
page 1065
Auto Cycle Fun With Sidebar Tabs
Wordpress make your plugin updateable?
one comment
page 1129
Post Image The Easy Peasy Way
Wp add attachment?
26 comments
page 1065
Html 5 Gallery
Html 5 gallery?
6 comments
page 1305
Html 5 Gallery
Pictures html5?
6 comments
page 1305
Html 5 Gallery
Pictures html5?
6 comments
page 1305
Wordpress Chat
Postimagetheeasypeasyway?
3 comments
page 1308
Using Your Own Url Shortener
How to flushrewriterules?
4 comments
page 1190
Custom Hooks For Admin Pages
Addaction adminpost?
one comment
page 430
Custom Hooks For Admin Pages
Addaction adminpost?
one comment
page 430
  1 query every 889 seconds, updated 1 seconds ago.
Post a comment?