WordPress.org

Make WordPress Core

Changeset 45798


Ignore:
Timestamp:
08/14/2019 11:02:25 PM (6 weeks ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets.php

    r45674 r45798  
    11261126    $instance = wp_parse_args( $instance );
    11271127
     1128    /** This filter is documented in wp-includes/class-wp-widget.php */
     1129    $instance = apply_filters( 'widget_display_callback', $instance, $widget_obj, $args );
     1130
     1131    if ( false === $instance ) {
     1132        return;
     1133    }
     1134
    11281135    /**
    11291136     * Fires before rendering the requested widget.
  • trunk/tests/phpunit/tests/widgets.php

    r45607 r45798  
    714714
    715715    /**
     716     * @ticket 34226
     717     */
     718    public function test_the_widget_should_short_circuit_with_widget_display_callback() {
     719        add_filter( 'widget_display_callback', '__return_false' );
     720
     721        register_widget( 'WP_Widget_Text' );
     722
     723        ob_start();
     724        the_widget( 'WP_Widget_Text' );
     725        $widget_content = ob_get_clean();
     726        unregister_widget( 'WP_Widget_Text' );
     727
     728        $this->assertEmpty( $widget_content );
     729    }
     730
     731    /**
    716732     * Register nav menu sidebars.
    717733     *
Note: See TracChangeset for help on using the changeset viewer.