How to extend a custom widget in wordpress?

别来无恙 提交于 2019-12-12 02:57:42

问题


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:


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.



来源:https://stackoverflow.com/questions/35612812/how-to-extend-a-custom-widget-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!