diff --git a/src/wp-includes/widgets.php b/src/wp-includes/widgets.php index c3be679516..674e2d78ec 100644 --- a/src/wp-includes/widgets.php +++ b/src/wp-includes/widgets.php @@ -1125,6 +1125,13 @@ function the_widget( $widget, $instance = array(), $args = array() ) { $instance = wp_parse_args( $instance ); + /** This filter is documented in wp-includes/class-wp-widget.php */ + $instance = apply_filters( 'widget_display_callback', $instance, $widget_obj, $args ); + + if ( false === $instance ) { + return; + } + /** * Fires before rendering the requested widget. * diff --git a/tests/phpunit/tests/widgets.php b/tests/phpunit/tests/widgets.php index 4d404100f6..8e38b30a60 100644 --- a/tests/phpunit/tests/widgets.php +++ b/tests/phpunit/tests/widgets.php @@ -712,6 +712,22 @@ class Tests_Widgets extends WP_UnitTestCase { the_widget( 'Widget_Class' ); } + /** + * @ticket 34226 + */ + public function test_the_widget_should_short_circuit_with_widget_display_callback() { + add_filter( 'widget_display_callback', '__return_false' ); + + register_widget( 'WP_Widget_Text' ); + + ob_start(); + the_widget( 'WP_Widget_Text' ); + $widget_content = ob_get_clean(); + unregister_widget( 'WP_Widget_Text' ); + + $this->assertEmpty( $widget_content ); + } + /** * Register nav menu sidebars. *