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
Resizing images in thecontent?
26 comments
page 1065
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Wordpress first image gallery?
26 comments
page 1065
Post Image The Easy Peasy Way
Inserting an image url in comments?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp attachment functions?
26 comments
page 1065
Using Your Own Url Shortener
String shortner?
4 comments
page 1190
Dont Mess With My Toot Toot
Wordpress custom content?
16 comments
page 599
Quick N Dirty Admin Login Screen
My admin login page?
no comment
page 128
Using Your Own Url Shortener
How to build a shortner link?
4 comments
page 1190
Post Image The Easy Peasy Way
Get post attacments?
26 comments
page 1065
Theming Habari Vs Wordpress
Habari vs wordpress?
13 comments
page 440
Are Child Themes The Best Option
Wp child themes?
15 comments
page 1262
Upload From Url
Upload to url?
6 comments
page 326
Silence Is Golden
Silence is golden wordpress?
3 comments
page 213
How To Add Sidebars To A Theme
Html hot add a side bar?
10 comments
page 1053
How To Add Sidebars To A Theme
How to add picture to the wordpress sidebar on sidebarphp?
10 comments
page 1053
Fun With Sidebar Tabs Styling
Position of the tabs thematic?
2 comments
page 336
Using Your Own Url Shortener
Short url?
4 comments
page 1190
Why I Ditched Disqus
Styling disqus widgets?
5 comments
page 1175
Wordpress 25 Exif Fields
Wordpress exif?
12 comments
page 230
Html 5 Gallery
Html 5 gallery?
6 comments
page 1305
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Fun With Sidebar Tabs Styling
Tabs with html css on same page javascript?
2 comments
page 336
Converting Wordpress Themes To Habari
Habari timthumb?
one comment
page 694
Wordpress 25 Exif Fields
Habari timthumb?
12 comments
page 230
Using Your Own Url Shortener
How to have own url short?
4 comments
page 1190
Post Image The Easy Peasy Way
Insert conditional image php wp?
26 comments
page 1065
Using Your Own Url Shortener
Run short url using htaccess?
4 comments
page 1190
Html 5 Gallery
Html5 simple wordpress theme?
6 comments
page 1305
Dont Mess With My Toot Toot
New posttype not displayed wordpress?
16 comments
page 599
Quick N Dirty Replacement Text
Wpupdatepost object?
no comment
page 122
How To Add Sidebars To A Theme
Wordpress sidebar above main sidebars?
10 comments
page 1053
  1 query every 1965 seconds, updated 1 seconds ago.
Post a comment?