Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Im trying to override the text widget in WordPress. My code looks like this:

class WP_Widget_Text_Custom extends WP_Widget {

    function __construct() {
        $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
        $control_ops = array('width' => 400, 'height' => 350);
        parent::__construct('text', __('Text'), $widget_ops, $control_ops);
    }

    function WP_Widget_Calendar( $args, $instance ) {
        extract($args);
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $before_widget;
        if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
            <div class="textwidget test"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
        <?php
        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        if ( current_user_can('unfiltered_html') )
            $instance['text'] =  $new_instance['text'];
        else
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
        $instance['filter'] = isset($new_instance['filter']);
        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
?>
        <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 esc_attr($title); ?>" /></p>

        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
    }
}

function custom_register_widgets() {
    register_widget( 'WP_Widget_Text_Custom' );
}

add_action( 'widgets_init', 'custom_register_widgets' );

I'm getting the following error message:

*function WP_Widget::widget() must be over-ridden in a sub-class.*

I have copied the WP_Widget_Text from default-widgets.php to functions.php and added _Custom in class name.

Why am I getting this error and how do I fix it?

share|improve this question
    
Instead of extending WP_Widget, try to extend the class you are trying to modify, WP_Widget_Text. –  Chloe Aug 10 at 22:12

1 Answer 1

You are getting this error because your class ass no method named widget which is a must to properly use the widgets api. Meaning that each class/widget that extends the WP_Widget class must have a method named widget which is responsible for the actual widget display. So consider this structure as a widget class skeleton:

class custom_Widget extends WP_Widget {
    public function __construct() {}// widget actual processes
    public function widget( $args, $instance ) {} // outputs the content of the widget
    public function form( $instance ) {} // outputs the options form on admin
    public function update( $new_instance, $old_instance ) {} // processes widget options to be saved
}

And in your case you have all but the widget method which to answer your second question (how to fix?) simply reanme your WP_Widget_Calendar method to widget.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.