1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Docs] Making a Plugin - Case Study 2: Hello World Widget

Discussion in 'Developing Plugins' started by Nick, Aug 8, 2009.

  1. Nick

    Nick Well-Known Member

    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:
    /**
     * 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:
    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:
    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.
     
    Last edited: Jan 8, 2010

Share This Page