Make WordPress Core

Changeset 41327


Ignore:
Timestamp:
09/01/2017 08:49:50 AM (7 years ago)
Author:
obenland
Message:

Widgets: Add nudge for registered widgets

Informs developers that widgets need to be registered before they can be
displayed through the_widget(). Previously it would fail with an ambiguous
undefined index notice.

Props SeBsZ, mrasharirfan.
Fixes #41743.

Location:
trunk
Files:
2 edited

Legend:

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

    r40926 r41327  
    10371037    global $wp_widget_factory;
    10381038
     1039    if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) {
     1040        _doing_it_wrong( __FUNCTION__, __( 'Widgets need to be registered before they can be displayed.' ), '4.9.0' );
     1041        return;
     1042    }
     1043
    10391044    $widget_obj = $wp_widget_factory->widgets[$widget];
    10401045    if ( ! ( $widget_obj instanceof WP_Widget ) ) {
  • trunk/tests/phpunit/tests/widgets.php

    r41266 r41327  
    680680
    681681    /**
     682     * Tests that no 'Undefined index' exception is thrown when trying to
     683     * display an unregistered widget.
     684     *
     685     * @see \the_widget()
     686     */
     687    function test_the_widget_with_unregistered_widget() {
     688        $this->setExpectedIncorrectUsage( 'the_widget' );
     689        the_widget( 'Widget_Class' );
     690    }
     691
     692    /**
    682693     * Register nav menu sidebars.
    683694     *
Note: See TracChangeset for help on using the changeset viewer.