WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 2 months ago

#34226 closed enhancement (fixed)

Add 'widget_display_callback' filter to the_widget()

Reported by: MarcGuay Owned by: SergeyBiryukov
Milestone: 5.3 Priority: normal
Severity: normal Version: 4.3.1
Component: Widgets Keywords: has-patch
Focuses: template Cc:
PR Number:

Description

When a widget is output through a registered sidebar it passes through the 'widget_display_callback' filter (see 'display_callback' in 'wp-includes/widgets.php', allowing for short-circuiting. This is not the case when 'the_widget' is called, however. This patch simply adds the same feature to that function.

The reason I came across this is because I am using the Display Widgets plugin to hide widgets under certain circumstances. It hooks into 'widget_display_callback', runs some logic, and returns false if it should not appear. Using 'the_widget' in a template file, I noticed that the widget still appeared.

It seems logical to me that any time a widget is displayed, it should go through this filter.

Attachments (3)

the_widget_filter.patch (985 bytes) - added by MarcGuay 4 years ago.
the_widget_filter-2.patch (958 bytes) - added by MarcGuay 4 years ago.
Patch updated for 4.4 and changed $this to $widget_obj in the docblock.
34226.3.diff (1.6 KB) - added by donmhico 2 months ago.
Refreshed the patch and added unit test.

Download all attachments as: .zip

Change History (7)

#1 @welcher
4 years ago

  • Keywords needs-unit-tests added

Thanks for the ticket and welcome!

This seems like a good idea to me - my only hesitation would be backwards compat. Getting some unit tests on this to confirm that the patch doesn't break anything would go a long way.

The 2nd $widget_obj in the patch doesn't match the doc block as well.

@MarcGuay
4 years ago

Patch updated for 4.4 and changed $this to $widget_obj in the docblock.

@donmhico
2 months ago

Refreshed the patch and added unit test.

#2 @donmhico
2 months ago

  • Keywords has-patch added; needs-unit-tests removed

#3 @SergeyBiryukov
2 months ago

  • Milestone set to 5.3
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#4 @SergeyBiryukov
2 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 45798:

Widgets: Allow for short-circuiting widget output in the_widget() using the widget_display_callback filter, for consistency with output via a registered sidebar.

Props MarcGuay, donmhico.
Fixes #34226.

Note: See TracTickets for help on using tickets.