You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
9.0.1: admin/links/class-link-watcher.php calling apply_filter the_content outside of The Loop, without context setup, is probably still a bad practice
#11395
Open
3 of 11 tasks
lkraav opened this issue
Oct 24, 2018
· 2 comments
WordPress core has, for years, relied on the convention that the_content filters are meant to be run inside a loop context with a valid global $post object. Multiple WordPress core functions already make that assumption, like gallery_shortcode() and prepend_attachment(), as well as all our embed handlers (which do caching by post ID). Any third party shortcode that touches post meta will be in the same boat, and won't work if $post isn't as expected.
It's not very clean, but it is expected and well-established behavior.
Please describe what you expected to happen and why.
There might be only one hypothetically low-cost improvement option:
Manually set up global $post while executing your apply_filters the_content call + clean it up after, to allow all "downstream" plugins to properly process incoming data.
Pseudo-code (or maybe real code?):
privatefunctionprocess( $post_id, $content ) {
global$post;
$post = get_post( $post_id );
setup_postdata( $post );
// Apply the filters to have the same content as shown on the frontend.$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
wp_reset_postdata();
$this->content_processor->process( $post_id, $content );
}
Otherwise you're breaking your own function promise of // Apply the filters to have the same content as shown on the frontend., which simply cannot happen if all plugins involved in the chain don't have the core context setup they have a right to expect.
Run wp_update_post() somewhere to trigger class-link-watcher.php
Technical info
WordPress version: 4.9.8
Yoast SEO version: 9.0.1
If relevant, which editor is affected (or editors):
Classic Editor
Gutenberg
Classic Editor plugin
Which browser is affected (or browsers):
IE11
Edge
Chrome
Firefox
Safari
WP-CLI
Relevant plugins in case of a bug:
Tested with theme:
The text was updated successfully, but these errors were encountered:
lkraav
changed the title
9.0.1: admin/links/class-link-watcher.php calling apply_filter the_content outside of The Loop is probably still a bad practice
9.0.1: admin/links/class-link-watcher.php calling apply_filter the_content outside of The Loop, without context setup, is probably still a bad practice
Oct 24, 2018
Please give us a description of what happened.
Some plugins are reasonably expecting global
$post
context to be correctly set during executingthe_content
filter.If this filter is executed outside of The Loop, you're likely to generate a ton of PHP Notices like
$post->ID trying to get property of non-object
.This has been discussed a while ago https://core.trac.wordpress.org/ticket/24330#comment:3
Notable quote from @markjaquith reads:
Please describe what you expected to happen and why.
There might be only one hypothetically low-cost improvement option:
Manually set up
global $post
while executing yourapply_filters the_content
call + clean it up after, to allow all "downstream" plugins to properly process incoming data.Pseudo-code (or maybe real code?):
Otherwise you're breaking your own function promise of
// Apply the filters to have the same content as shown on the frontend.
, which simply cannot happen if all plugins involved in the chain don't have the core context setup they have a right to expect.How can we reproduce this behavior?
wp_update_post()
somewhere to triggerclass-link-watcher.php
Technical info
The text was updated successfully, but these errors were encountered: