[wp-trac] [WordPress Trac] #42548: Widgets: Populate global $post with queried object on singular queries instead of nullifying it

WordPress Trac noreply at wordpress.org
Tue Nov 14 18:03:26 UTC 2017


#42548: Widgets: Populate global $post with queried object on singular queries
instead of nullifying it
--------------------------+------------------
 Reporter:  westonruter   |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  4.9
Component:  Widgets       |     Version:  4.9
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:
--------------------------+------------------
Description changed by westonruter:

Old description:

> Until 4.9, if a plugin did `add_filter( 'widget_text', 'do_shortcode' )`
> and they added a `[gallery]` to a widget that is displayed on a singular
> template, then the gallery would (usually) show the attachments from the
> current queried post. This behavior changed in #10457 for 4.9 whereby the
> global `$post` is nullified to ensure that the Text widget renders
> consistently across whatever template it may appear (singular, archive,
> 404, etc).
>
> There are two problems with this:
>
> 1. When `$post` is `null`, then the gallery shortcode will list out all
> attachments from the media library. This could be embarrassing or at
> least undesired.
> 2. If a site previously did rely on being able to render a gallery of
> attached images for the current post in the sidebar, this is broken.
>
> I think we can fix both of these issues simply by checking if
> `is_singular()` and if so, set the `$post` to the current queried object
> (`get_queried_object()`). This will ensure that the global `$post` isn't
> coming from an unpredictable source (such as a previous query that failed
> to call `wp_reset_postdata()`), which was a primary motivator for
> nullifying the global `$post` to begin with.
>
> This same approach can be done for the Custom HTML widget, which
> currently doesn't nullify `$post` even though it does apply `widget_text`
> filters (though not for shortcodes in core).

New description:

 Until 4.9, if a plugin did `add_filter( 'widget_text', 'do_shortcode' )`
 and they added a `[gallery]` to a widget that is displayed on a singular
 template, then the gallery would (usually) show the attachments from the
 current queried post. This behavior changed in #10457 for 4.9 whereby the
 global `$post` is nullified to ensure that the Text widget renders
 consistently across whatever template it may appear (singular, archive,
 404, etc).

 There are two problems with this:

 1. When `$post` is `null`, then the gallery shortcode will list out all
 attachments from the media library. This could be embarrassing or at least
 undesired.
 2. If a site previously did rely on being able to render a gallery of
 attached images for the current post in the sidebar, this is broken.

 I think we can fix both of these issues simply by checking if
 `is_singular()` and if so, set the `$post` to the current queried object
 (`get_queried_object()`). This will ensure that the global `$post` isn't
 coming from an unpredictable source (such as a previous query that failed
 to call `wp_reset_postdata()`), which was a primary motivator for
 nullifying the global `$post` to begin with.

 This same approach can be done for the Custom HTML widget, which currently
 doesn't nullify `$post` even though it does apply `widget_text` filters
 (though not for shortcodes in core). This would close #42547.

--

--
Ticket URL: <https://core.trac.wordpress.org/ticket/42548#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list