XHanch Studio Log in | Register | Cart

Forum

[Tutorial] Developi...
 
Notifications
Clear all

[Tutorial] Developing a multiple-instance widget in WordPress

1 Posts
1 Users
0 Likes
969 Views
XHanch
(@xhanch-alt)
Posts: 2105
Member Admin
Topic starter
 

To create a widget, you only need to extend the standard WP_Widget class and some of its functions.

That base class also contains information about the functions that must be extended to get a working widget.

The WP_Widget class is located in wp-includes/widgets.php.

Here is the basic structure to create a widget:

class widget_name extends WP_Widget{
    function widget_name() {
        //widget actual processes
    }

    function form($instance) {
        //outputs the options form on admin
    }

    function update($new_instance, $old_instance) {
        //processes widget options to be saved
    }

    function widget($args, $instance) {
        //outputs the content of the widget
    }
}
register_widget('widget_name');

As an example, here is the code that will create a widget named test_widget that has a settings form to change the display title:

/**
* test_widget Class
*/
class test_widget extends WP_Widget {
    /** constructor */
    function test_widget() {
        parent::WP_Widget(false, $name = 'test_widget');
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                        echo $before_title . $title . $after_title; ?>
                  Hello, World!
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
        $title = esc_attr($instance['title']);
        ?>
        <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
        <?php
    }

} // class test_widget

This sample widget can then be registered in the widgets_init hook:

add_action('widgets_init', create_function('', 'return register_widget("test_widget");'));

If you use PHP 5.3. with namespaces you should call the constructor directly as in the following example:

namespace abc;
    class mywidgetclass extends WP_Widget {
        function __construct(){
            parent::__construct('name1', 'name2');
        }
        // ... rest of functions
    }

and call the register widget with:

add_action('widgets_init',function(){
    return register_widget('abcmywidgetclass');
});

That's all. You will automatically get a multi-widget. No special tweaks needed any longer for that.

 
Posted : 13/05/2011 2:17 am
Share:

× Close Menu