What's the difference between a plugin and a widget?
There isn't much difference at all. A Hotaru widget is a plugin which can be displayed in any widget block (the default theme's sidebar.php contains a widget block). Examples include the Categories Widget, Posts Widget, Search, and RSS feeds from the RSS Show plugin.
How to make a Hello World widget
Here are the steps you need to take to make the most basic of widgets, one that does nothing more than display "Hello World" in your sidebar. Of course, you could do this manually in about 5 seconds, but this way is much more fun!
1. Build the Hello World plugin
The best place to start is with the Hello World plugin. If you haven't already done so, go and make that first...
2. Change the Header info:
Update the description and version number, require the Widgets plugin, and a different hook to replace the old "hello_world" one:
PHP Code:
/**
* name: Hello World
* description: Displays "Hello World!" in the sidebar
* version: 0.2
* folder: hello_world
* class: HelloWorld
* requires: widgets 0.6
* hooks: install_plugin
*/
3. An install function
The install_plugin function will register your plugin as a widget when it's installed. Add it within your HelloWorld class.
PHP Code:
public function install_plugin($h)
{
// widget
$h->addWidget('hello_world', 'hello_world', ''); // plugin name, function name, optional arguments
}
4. Rename the old Hello World function
This function was originally called hello_world, but since we're not making a manual plugin hook for it anymore, we need to use a special function name for widgets: widget_hello_world
PHP Code:
public function widget_hello_world()
{
echo "Hello World!";
}
5. Install and enjoy!
Go to Plugin Management and install the Widgets and Hello World plugins if you haven't already. Installing it will register it as a widget and you can then go to Admin -> Widgets under Plugin Settings and move your widget around.
Bookmarks