-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove the_content filter for block_template_parts #20343
Remove the_content filter for block_template_parts #20343
Conversation
@@ -32,7 +32,7 @@ function render_block_core_template_part( $attributes ) { | |||
if ( is_null( $content ) ) { | |||
return 'Template Part Not Found'; | |||
} | |||
return apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this filter is needed to run dynamic blocks, shortcuts and other things in the content of the template part, what's the reason behind removing it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youknowriad I attempted to explain the problem in my description. Here's a screenshot that hopefully illustrates it:
https://user-images.githubusercontent.com/7538525/74973542-975e8800-53f1-11ea-9bdf-73a0cabdbea0.png
Imagine those red boxes are Social Sharing buttons, or purchase buttons from an ecommerce plugin (Easy Digital Downloads does this for example). You'll get duplicate output on the screen after every template part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really think that's an issue though, is it? You're just running the hook on all post types (all contents)?
An alternative here would be to have another the_template_content
or something like that, that does the same thing as the_content
but excludes third-party the_content
filters maybe.
i believe if we don't run the filter now, dynamic blocks inside template parts don't work properly right (I didn't test, just a guess) (among other things maybe, embeds, shortcodes...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youknowriad While we definitely can't use the_content
here due to many plugins authors using that for custom outputs over the years, I believe you're correct that we do need to do the same processing on the return. We should likely introduce a new filter like the one you described. How about calling it the_template_part
to make it specific for this context? If you agree I can add that to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like these hook-calls will need to be added to this new filter:
https://github.com/WordPress/wordpress-develop/blob/3fb063f7052bf7427f6c965e493c9b12f2a566c9/src/wp-includes/default-filters.php#L172-L178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense but I'd love thoughts from other folks cc @epiqueras @mcsf @azaozz
Also, I'm thinking some of these filters are not needed because they will apply after the fact to the output of the template part too on the parent template get_the_content calls. (depends on the priority I guess)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youknowriad You are correct that dynamic blocks and shortcodes do not work in the header.html file with this PR.
I took another shot at this here, where I've added a new/dedicated filter, instead of removing it entirely:
#20344
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we manually run the filters from https://github.com/WordPress/wordpress-develop/blob/3fb063f7052bf7427f6c965e493c9b12f2a566c9/src/wp-includes/default-filters.php#L172-L178 and see how we do before introducing a new filter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@epiqueras That works as well. Added here: 729a613
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we manually run the filters
Yeah, should be "good enough" for testing. The prepend_attachment
can be removed. It is an ancient hack (from WP 2.0) for when an attachment post is displayed. Doesn't do anything if the current post is not an attachment:
$post = get_post();
if ( empty( $post->post_type ) || 'attachment' !== $post->post_type ) {
return $content;
}
However this brings another question: most of these "display filters" are intended to run on the front-end for post_content
and expect things like $post = get_post();
(i.e. the PHP global $post to be set) in order to work. Even if core doesn't rely on the global, some plugins will. Seems this will need some "special handling" for when the new filter for template parts is added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this!
Uh, sorry, a bit late to the party :)
Yeah, thinking that'd be best. The old filter is generally expected to run only on
Big +1. The template parts only support blocks. Things like wpautop and out-of-block/old-style shortcode processing don't seem to make sense. Looking at the default filters: add_filter( 'the_content', 'do_blocks', 9 );
add_filter( 'the_content', 'wptexturize' );
add_filter( 'the_content', 'convert_smilies', 20 );
add_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'shortcode_unautop' );
add_filter( 'the_content', 'prepend_attachment' );
add_filter( 'the_content', 'wp_make_content_images_responsive' );
add_filter( 'the_content', 'do_shortcode', 11 ); only |
@azaozz I tested removing each thing you mentioned. Here are the results of each: Removing
|
Thanks for the sleuthing! |
Yeah, this is partially a "review" of how things work or don't work in template parts. You're right, here's not the best place. Haven't had the time yet, but will definitely try to find the discussion(s) for these.
If double line breaks have to be converted to paragraph tags, why would you expect that to happen dynamically on the front-end? This is intended for when editing HTML in plain textarea (no JavaScript involved at all). Is it even possible to do in a block inside the editor?
Same thing, why would template parts support this ancient, buggy, inefficient way to bypass dynamic blocks? Both wpautop and shortcodes have to work in post_content for historical reasons. But they don't make much sense outside of post_content. Supporting them in template HTML doesn't seem good. |
@azaozz Good points. Looking at it again, I agree about wpautop. But for shortcodes, unless we remove the shortcode block from Gutenberg, people can still use shortcodes, and that will include in their header. Without calling For wpautop, I tested the Classic Editor block to make sure it wouldn't require wpautop, and it does not, as it converted my multi-line breaks to I don't think there are any other "legacy" style blocks that would require wpautop either, so I think we should remove wpautop. |
Yeah, don't think autop is used anywhere else. (Also thinking it's about time to review the classic block and remove it from there too. The part that strips the
The shortcode block is back-compat for when editing |
@azaozz after giving it a lot of thought and discussing with a few people, I agree that shortcodes could be removed from block template parts. I took a shot at that here: However, I was not able to find a way to conditionally filter the available blocks based on context like you described.
|
|
$content = wpautop( $content ); | ||
$content = shortcode_unautop( $content ); | ||
$content = prepend_attachment( $content ); | ||
$content = wp_make_content_images_responsive( $content ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@youknowriad PR opened: #21514
Description
This PR fixes #20342, where content filtered to the_content appears after every block template part is output.
How has this been tested?
Types of changes
Bug fix.
Screenshots:
Before:
After:
Checklist: