gutenberg
gutenberg copied to clipboard
Post Title: Empty heading element when `post_title` is empty but `get_the_title` returns markup
Description
An empty heading element (or more specifically, a heading without text content) will be output from the Post Title block, if the post title returned from the loop has markup but no text content. The most likely case (only case?) is if a plugin/theme has filtered the_title
and adds markup around an empty post_title
.
The Post Title block currently does not output the heading if the post content is empty: https://github.com/WordPress/gutenberg/blob/v17.1.0/packages/block-library/src/post-title/index.php#L30
But in a case where the title has markup without text content, it will not be an empty string and will result with in an empty heading element output.
In my specific case, this is triggered by the wp-uf2 (Microformats) plugin, which filters the title to add markup: https://github.com/indieweb/wordpress-uf2/blob/master/includes/class-uf2-post.php#L65
Admitedly this could be handled by the plugin in some way, or by the theme. And in fact, in a classic theme this could/would have been handled there. However, I feel this case should be handled within the block, since it's generating the heading markup.
It was previously decided to not output the heading element if the title is empty. See: https://github.com/WordPress/gutenberg/issues/29520
So an option would be to handle this simply with strip_tags
:
if ( ! strip_tags( $title ) ) {
This way, the heading would only be output when there is content to go with it. That feels in line with the intention of the current code.
But I think a more appropriate option is to output a valid heading. The Post Title block is, presumably, used most often within a Query Loop block with other Post blocks (Post Excerpt, Date, etc.), so a heading should be output ahead of that content. If an article
is wrapping the post, the heading is required. But that's a more involved change that could use discussion.
Step-by-step reproduction instructions
- Publish a new post without a title
- On a page, add a Query Loop block with at least a Post Title
- Install and activate the plugin 'wp-uf2' (or filter
the_title
to add markup wrapping the post title) - View the front end of the page created in step 2, locate the post with the empty title
- View empty h2 in markup:
<h2 class="wp-block-post-title"><a href="_permalink_" target="_self"><span class="p-name"></span></a></h2>
Screenshots, screen recording, code snippet
No response
Environment info
- WordPress 6.4.1
- Gutenberg plugin is not installed
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
Hi, This issue has gone 30 days without any activity. This means it is time for a check-in to make sure it is still relevant. If you are still experiencing this issue with the latest versions, you can help the project by responding to confirm the problem and by providing any updated reproduction steps. Thanks for helping out.
I tested with the current Gutenberg trunk (e31be2f785) and it still reproduced.
I reproduced it by installing the wp-uf2 (Microformats) plugin or by adding the following code.
add_filter(
'the_title',
function( $title ) {
return '<strong>' . $title . '</strong>';
}
);