Support » Developing with WordPress » Code cruft in WP_Widget_Recent_Posts, WP_Widget_Recent_Comments ?

  • kitchin

    (@kitchin)


    Writing a “recent posts” widget with an extra feature (exclude cats), I noticed core WP_Widget_Recent_Posts and WP_Widget_Recent_Comments have this code at the top of widget():

    public function widget( $args, $instance ) {
    	if ( ! isset( $args['widget_id'] ) ) {
    		$args['widget_id'] = $this->id;
    	}

    Those are the only two core widgets with it, and no wordpress handbook etc. examples suggest anything like this is needed in a widget.

    It was added many years ago to protect a caching mechanism, no longer used, from getting an undefined array index. Only those two widgets were cached, it seems.

    Cruft? I see no reason to put it in my modified copy of the core widget, but then again the code calling widget() is a somewhat mysterious thicket.

    Before looking at the history, I thought customizer or legacy widget block might need it, but those are new. Grepping, core doesn’t seem to special-case these two widgets anywhere. Caching is done at the query level now, instead of the html level. These two widget classes were de-cached 7 years ago: https://core.trac.wordpress.org/ticket/27565 and a similar fixed ticket for the other one.

  • You must be logged in to reply to this topic.