Extending WP_Widget throws error when Monster Widget used

I wanted to override the output for the core Recent Comments Widget. I extended WP_Widget_Recent_Comments made the changes needed and it displays exactly how I want. However, I have now tested the theme with Monster widget, and for where the Recent Comments widget should be output an error is thrown:

Notice: Undefined index: WP_Widget_Recent_Comments in /Users/tfisher/Sites/mysite/dev/wp-includes/widgets.php on line 1319

Can’t figure out why it would only throw an error when used by Monster Widget.

Read More

Here’s my extension class:

Class My_Recent_Comments_Widget extends WP_Widget_Recent_Comments {

    function widget( $args, $instance ) {
        global $comments, $comment;

        $cache = wp_cache_get('widget_recent_comments', 'widget');

        if ( ! is_array( $cache ) )
            $cache = array();

        if ( ! isset( $args['widget_id'] ) )
            $args['widget_id'] = $this->id;

        if ( isset( $cache[ $args['widget_id'] ] ) ) {
            echo $cache[ $args['widget_id'] ];
            return;
        }

        extract($args, EXTR_SKIP);
        $output = '';

        $title = ( ! empty( $instance['title'] ) && isset($instance['title']) ) ? $instance['title'] : __( 'Recent Comments', 'mysite' );

        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

        $number = ( ! empty( $instance['number'] ) && isset($instance['number']) ) ? absint( $instance['number'] ) : 5;
        if ( ! $number )
            $number = 5;

        /**
         * Filter the arguments for the Recent Comments widget.
         *
         * @since 3.4.0
         *
         * @see get_comments()
         *
         * @param array $comment_args An array of arguments used to retrieve the recent comments.
         */
        $comments = get_comments( apply_filters( 'widget_comments_args', array(
            'number'      => $number,
            'status'      => 'approve',
            'post_status' => 'publish'
        ) ) );

        $output .= $before_widget;
        if ( $title )
            $output .= $before_title . $title . $after_title;

        if ( $comments ) {
            // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
            $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
            _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );

            foreach ( (array) $comments as $comment) {
                $output .= '<div class="recentcomments item clearfix">';
                if(get_avatar($comment)) {
                    $output .= '<figure class="pull-left">';
                    $output .= '<a href="' . get_comment_author_url() . '">';
                    $output .= get_avatar( $comment, 100 );
                    $output .= '</a>';
                    $output .= '</figure>';
                    $output .= '<div class="content">';
                } else {
                    $output .= '<div class="content no-img">';
                }
                $output .= sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) )) . '">';
                $output .= get_the_title($comment->comment_post_ID) . '</a>';
                $output .= '</div>'; // content
                $output .= '</div>'; // item
            }
        }
        $output .= $after_widget;

        echo $output;
        $cache[$args['widget_id']] = $output;
        wp_cache_set('widget_recent_comments', $cache, 'widget');
    }
}

Related posts

Leave a Reply

1 comment

  1. Monster Widget is a bit of a trap like that. Despite how it looks it doesn’t actually place “real” widgets into sidebar.

    It is a single widgets that creates output of other widgets inside of itself. If you are significantly interfering with its expectations then it won’t pick up on changes, since it’s not engineered to do it.

    You will likely need to use its monster-widget-config filter to modify configuration, excluding native widget and splicing your own in.