Skip to content

Conversation

@shubhtoy
Copy link

What?

Closes #56465

Fix empty heading element when post_title is empty but get_the_title returns markup by checking for text content after stripping HTML tags.

Why?

The Post Title block was outputting empty heading elements when plugins or themes filter the_title to add markup around an empty post title. This creates invalid HTML with headings that contain no text content, which can cause accessibility issues and semantic problems.

The specific issue occurred when:

  1. A post has an empty title
  2. A plugin/theme filters the_title to wrap the empty title with HTML markup (e.g., <span class="p-name"></span>)
  3. The Post Title block would output something like <h2><span class="p-name"></span></h2> - an empty heading

This was particularly problematic with plugins like wp-uf2 (Microformats) that add semantic markup to post titles.

How?

Updated the condition in render_block_core_post_title() to check not only if the title is empty, but also if the title has no text content after stripping HTML tags using strip_tags().

The fix changes:

if ( ! $title ) {
    return '';
}

To:

if ( ! $title || ! strip_tags( $title ) ) {
    return '';
}

This ensures that if the title contains only HTML markup without any actual text content, the heading element will not be rendered at all, maintaining the original intention of not outputting empty headings.

Testing Instructions

  1. Create a new post without a title
  2. Add a Query Loop block to a page with a Post Title block inside
  3. Add the following code to your theme's functions.php or create a test plugin:
    add_filter(
        'the_title',
        function( $title ) {
            return '<strong>' . $title . '</strong>';
        }
    );
  4. View the frontend page and inspect the HTML
  5. Verify that no empty heading element is rendered for the post with no title

Alternatively, test with the wp-uf2 (Microformats) plugin:

  1. Install and activate the wp-uf2 plugin
  2. Follow steps 1-2 above
  3. View the frontend and verify no empty <h2><a href="..."><span class="p-name"></span></a></h2> is rendered

Screenshots or screencast

Before

9a79-7b6a-4fc6-99f0-0d788ac74ed8.mp4

After

1811-acfd-44da-a816-9b72830bff88.mp4

@github-actions
Copy link

github-actions bot commented Jun 13, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @[email protected].

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: [email protected].

Co-authored-by: SirLouen <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: inc2734 <[email protected]>
Co-authored-by: shubhtoy <[email protected]>
Co-authored-by: jessedyck <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Jun 13, 2025
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @shubhtoy! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Block] Post Title Affects the Post Title Block labels Jun 16, 2025
Copy link
Member

@SirLouen SirLouen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A review before testing

$title = get_the_title();

if ( ! $title ) {
if ( ! $title || ! strip_tags( $title ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the second part is true, the first part is always going to be true.
And if the second part is false the first part is always going to be false

And get_the_title always returns a string
https://developer.wordpress.org/reference/functions/get_the_title/

So the first part can technically be removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shubhtoy can you update the PR to only include the strip_tags check, as @SirLouen mentions. We could land afterwards.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ntsekouras you can merge this:
shubhtoy#1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ntsekouras you can merge this:

Not sure I could do that since it's the @shubhtoy's fork (permissions). @SirLouen I guess you could open a new PR, close this one and probably add @shubhtoy in props. There wasn't a reply for months since your comment (Jul 30) and there might not be in the future too :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ntsekouras just FTR, can you go into that and see if you have the merge available?
Theoretically, if you have write permissions here, and the creator of the PR has enabled maintainers to edit, you should be able to do the merge since it's the specific branch..
Confirm if you don't have this option here shubhtoy#1, and I will create a new PR but it would be good to know it.
FWIW, the other day, one maintainer did exactly this in a core Git PR, and I wondered if it's universal or only available to certain maintainers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you go into that and see if you have the merge available?

Sorry for not clarifying yesterday. I commented after going there and seeing that I couldn't merge. After searching it seems that this needs to be true:

the creator of the PR has enabled maintainers to edit

and the creator probably hasn't.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ntsekouras ok, created new #73841

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the second part is true, the first part is always going to be true. And if the second part is false the first part is always going to be false

And get_the_title always returns a string https://developer.wordpress.org/reference/functions/get_the_title/

So the first part can technically be removed.

When the first part is true in | | condition, the second part won't be evaluated. Which is handy when following in checks in condition can be expensive.

Here's an example - https://3v4l.org/mV5CS#vnull.

@Mamaduka
Copy link
Member

Mamaduka commented Dec 9, 2025

Thank you, @shubhtoy!

I'm going to close in favor of #73841. The PR will include all associated props.

@Mamaduka Mamaduka closed this Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Post Title Affects the Post Title Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Post Title: Empty heading element when post_title is empty but get_the_title returns markup

5 participants