wordpress-sidebar-widget

WordPress widgets is a great feature for displaying your content in sidebar. It is easy to create by extending WP_Widget predefined wordpress class.

Constructing Widget

class your_widget extends WP_Widget {
    public function __construct() {
        $this->WP_Widget(
            'your_widget_unique_id',
            __('Your Widget Title'),
            array('description' => __('Your Widget brief description', 'your_widget_unique_id'))
        );
    }

    public function widget( $args, $instance ) {
        extract($args);
        $title = esc_attr($instance['title']);
        echo $before_widget.$before_title.'Your Widget Sidebar Heading'.$after_title;
        if ( function_exists('your_custom_function') ){
            your_custom_function();
        }
        echo $after_widget;
    }
}

Widget function call

function your_widget_init() {
    register_widget('your_widget');
}

Hook your widget function call

add_action('widgets_init', 'your_widget_init');

Leave a Reply