PHPBuilder - Customize Your WordPress Blog with PHP Plugins and Widgets



RSS Twitter
Articles Site Operation

Customize Your WordPress Blog with PHP Plugins and Widgets

by: Keith Vance
|
March 11, 2010

There's nothing more boring than a blog that looks just like one of the millions of other blogs floating around the web. Fortunately for WordPress users, plugins and widgets make adding custom functionality to your blog easy. Think of plugins as components where you put your functionality and widgets as components of your user interface. Building your own WordPress plugins and widgets will make your blog truly original, and all you need is basic PHP and HTML knowledge—and your imagination.
In this tutorial, you'll build a simple plugin and widget combo application called BoobTube. BoobTube simply uses a widget to display the newest YouTube video from your blog's YouTube channel. Download the source code to follow along with the instructions.

Building the WordPress Plugin

The first step in building a WordPress plugin is to pick a unique name. In this case, you're going to call it BoobTube, but when you build your own plugin, make sure you choose a name that is not already taken. A duplicate name will cause problems in the WordPress plugin manager, even if you don't distribute your plugin publicly.
Create a directory in wp-content/plugins called boobtube, and create a file called boobtube.php in it. Boobtube.php will be your main plugin file where all your PHP code resides.
Listing 1 shows the entire boobtube.php file. All plugins should include two header components, one for standard plugin information and the other for licensing terms. Even if you don't intend to distribute your plugin, completing the information for these components is good WordPress style.

Walking Through the Plugin Code

First, you have to define a few constants. The first constant is setting up a nonce key.
define('BOOBTUBE_ADMIN_NONCE', 'boobtube-adminpanel-0122');
A nonce is a number used once. It's used to verify that the data submitted via the administrator plugin panel actually comes from the administrator panel rather than from a hacker trying to mess with your plugin.
Next, you set up a few defaults for the BoobTube plugin and widget.
define('BOOBTUBE_HEIGHT', '151');
define('BOOBTUBE_WIDTH', '250');
define('BOOBTUBE_YOUTUBEID', 'Pn-G_pYJJfU');
define('BOOBTUBE_UPDATE_INTERVAL', '10');
define('BOOBTUBE_USERNAME', 'goodganews');
It's always a good idea to verify that your class doesn't already exist before you define it. This prevents naming collisions with other plugins.
if (!class_exists("BoobTube")) {
    class BoobTube {
    ...
    }
}
After you've setup your class and methods, instantiate the class. Again, just to be safe and to avoid a fatal error, make sure the class exists before instantiating it.
if (class_exists("BoobTube")) {
    $boobtube = new BoobTube();
}

Registering the Necessary WordPress Actions

The WordPress documentation says: "actions are triggered by specific events that take place in WordPress, such as publishing a post, changing themes, or displaying a page of the admin panel. Your plugin can respond to the event by executing a PHP function."
To make the BoobTube plugin work, you need to tell WordPress how to run it.
add_action('boobtube/boobtube.php', array(&$boobtube, 'init'));
This tells WordPress to activate boobtube.php, passes it a reference to the $boobtube class you instantiated earlier, and it tells WordPress which method to call. In this case, you tell WordPress to call init, but when you build your own plugin you can call whatever method is appropriate for your implementation.
BoobTube has an administrator panel, so you need to register that action too.
add_action('admin_menu', 'BoobTube_AdminPanel'); 
BoobTube_AdminPanel is simply a function that calls the built-in WordPress function add_options_page described in the WordPress documentation as:
add_options_page(page_title, menu_title, capability, handle, [function]); if (!function_exists('BoobTube_AdminPanel')) { function BoobTube_AdminPanel() { if (!$GLOBALS['boobtube']) { return; } if (function_exists('add_options_page')) { add_options_page('BoobTube Admin Panel', 'BoobTube', 9, basename(__FILE__), array(&$GLOBALS['boobtube'], 'printAdminPanel')); } } }
The last action to register is the widget. Not all plugins require a widget, but BoobTube does. To register a widget, you register a widgets_init action.
add_action('widgets_init', 'BoobTube_Widget');
To learn more about WordPress actions, see the Plugin API documentation.

1
|
2
Next Page »

Download: boobtube.zip

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Keith Vance

Comment:



Comment:

(Maximum characters: 1200). You have characters left.