How to extend a custom widget in wordpress?

3.4k views Asked by At

I am talking about making a widget which extends a child widget of WP_Widget.

Using this as reference: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice

Example:

My_WidgetBase extends WP_Widget

My_Widget extends My_WidgetBase

I want to know how to get My_Widget to show up with my other widgets (it is not currently). I have gotten My_WidgetBase to work.

This is important for my framework. I understand that widget(), update(), and form() must be overridden, but I could not find anything about making a grandchild of WP_Widget anywhere.

Example:

class My_WidgetBase extends WP_Widget {

    public function __construct($id = 'my-widget-base', $desc = 'My WidgetBase', $opts = array()) {
        $widget_ops = array();
        parent::__construct( $id, $desc, $widget_ops );
    }

    public function widget( $args, $instance ) {
        die('function WP_Widget::widget() must be over-ridden in a sub-class.');
    }


    public function update( $new_instance, $old_instance ) {
        return $new_instance;
    }


    public function form( $instance ) {
        echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
        return 'noform';
    }

}

    function RegisterMyWidgets() {
        register_widget('My_WidgetBase');
    }

add_action( 'widgets_init', 'RegisterMyWidgets' );
1

There are 1 answers

0
aus On BEST ANSWER

Solved digging further into this thread: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice

The problem was not with my implementation of the classes, but how I included the parent in my framework. I was not working with both child and grandchild in the same php file, but two separate files with different directories.

Solving this problem was a matter of using require_once() correctly in the directory of my grandchild class.