WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 2 years ago

#34226 new enhancement

Add 'widget_display_callback' filter to the_widget()

Reported by: MarcGuay Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.3.1
Component: Widgets Keywords: needs-unit-tests
Focuses: template Cc:

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 (2)

the_widget_filter.patch (985 bytes) - added by MarcGuay 3 years ago.
the_widget_filter-2.patch (958 bytes) - added by MarcGuay 2 years ago.
Patch updated for 4.4 and changed $this to $widget_obj in the docblock.

Download all attachments as: .zip

Change History (3)

#1 @welcher
2 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
2 years ago

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

Note: See TracTickets for help on using tickets.