#42548 closed defect (bug) (fixed)
Widgets: Populate global $post with queried object on singular queries instead of nullifying it
Reported by: | westonruter | Owned by: | westonruter |
---|---|---|---|
Milestone: | 4.9 | Priority: | normal |
Severity: | normal | Version: | 4.9 |
Component: | Widgets | Keywords: | has-patch dev-reviewed commit |
Focuses: | Cc: |
Description (last modified by )
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:
- When
$post
isnull
, then the gallery shortcode will list out all attachments from the media library. This could be embarrassing or at least undesired. - 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.
Attachments (9)
Change History (24)
This ticket was mentioned in Slack in #core by westonruter. View the logs.
7 years ago
@
7 years ago
Change gallery_shortcode to not display unattached images when used in a Text or Custom HTML widget. Support id=0 when this is required.
#6
@
7 years ago
Manually tested with posts with and without attached images. Widgets in the primary sidebar include a Custom HTML widget containing [gallery] and a Text widget containing the same shortcode.
The posts also contain the [gallery] shortcode.
One of the posts contains a [gallery id=0] shortcode.
When viewing the home page archive the widgets are empty.
When viewing a singular post the widgets show the attached images.
#7
@
7 years ago
- Keywords 2nd-opinion added
I combined the two latest patches and cleaned up some formatting in 42548.3.diff.
I'll need a 2nd opinion from media team on the gallery changes.
This ticket was mentioned in Slack in #core by westonruter. View the logs.
7 years ago
#9
@
7 years ago
42548.4.diff ensures that the original $post->ID
is supplied in the call to shortcode_atts
so that it is available for shortcode_atts_gallery
filters. But instead of passing 0
when there is no $post
it instead passes null
. If the null
is then what gets returned, the gallery_shortcode()
function will then short-circuit if no ids
are supplied either.
#10
@
7 years ago
- Keywords dev-feedback added; 2nd-opinion removed
42548.6.diff undoes the changes to gallery_shortcode()
, since the changes weren't able to apply to plugins that filter post_gallery
to override the default gallery behavior. In a future release we may want to revisit this to see if there is a better way to contextually render galleries when they are rendered in non-singular templates.
In ≤4.8, gallery shortcodes without explicit ids
appearing in Text widgets (when a plugin adds support shortcode support) already have the behavior: when a theme resets the $post
global or it is not set to begin with (such as on the 404 template) then the gallery widget currently will list out all attachments. In #10457 this behavior was applied to every widget in whatever context, even singulars. So 42548.6.diff makes sure that the $post
is consistently the queried object (post) when is_singular()
. Even before 4.9 if a user intended to have a widget appear in a sidebar and for it to rely on the post context, they should have already been using a sidebar that is dedicated for the singular template, or else use something like Jetpack Visibility to prevent the widget from appearing on anything aside from singular queries.
See full discussion with arguments for why gallery_shortcode()
remains unchanged in 4.9: https://wordpress.slack.com/archives/C02RQBWTW/p1510703055000030
#13
@
7 years ago
- Keywords dev-reviewed added; dev-feedback removed
Based on my testing, and extensive conversation in Slack, I'm +1 on the approach in 42548.8.diff for 4.9.
Unless I've applied the patch incorrectly my tests indicate that gallery_shortcode needs changing to test if the $id is not empty.
Without this test, when viewing an archive, the [gallery] shortcode will still produce a display of all the unattached images. This applies to both Custom HTML widgets and Text widgets.
I've developed an additional patch that caters for this problem.
It also allows [gallery id=0] to display all the unattached images.
This patch is in addition to 42548.1.diff