Results 1 to 1 of 1

Thread: Making a Plugin - Case Study 2: Hello World Widget

  1. #1
    Former lead dev Nick's Avatar
    Join Date
    Jun 2009
    Location
    Kakamigahara, Japan
    Posts
    2,914
    Blog Entries
    88
    Thanks
    519
    Thanked 845 Times in 550 Posts

    Default Making a Plugin - Case Study 2: Hello World Widget

    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.
    Last edited by Nick; 01-08-2010 at 03:38 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Docs] How to Add Another Sidebar
    By Nick in forum Design and Layout
    Replies: 0
    Last Post: 08-08-2009, 02:45 PM
  2. [Docs] Making a Plugin - Case Study 1: Hello World
    By Nick in forum Developing Plugins
    Replies: 0
    Last Post: 06-18-2009, 05:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •