Skip to content

Post Content Block: Add tagName selector #70698

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

Merged
merged 9 commits into from
Jul 16, 2025
Merged

Conversation

EliezerSPP
Copy link
Contributor

What?

Add the "tagName" selector to the Post Content Block.

Why?

This PR introduces the possibility to change the block's default wrapper (div) to a more semantic alternative (main, section, and article) optionally.

How?

This code is based on the group tag selector.

Testing Instructions

  1. Open the site editor.
  2. Insert the post content block.
  3. Open the block settings.
  4. Click on the advanced section.
  5. That's all! There is now a new selector available to change the default block wrapper.

Screenshots

image

Copy link

github-actions bot commented Jul 11, 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.

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

Co-authored-by: EliezerSPP <eliezerspp@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>

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

@t-hamano t-hamano added [Type] Enhancement A suggestion for improvement. [Block] Post Content Affects the Post Content Block labels Jul 12, 2025
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Adding a tagName attribute to a post content block makes a lot of sense to me.

However, we don't need to use the SelectControl component itself. There is already a handy wrapper component called HTMLElementControl. This component also has a functionality to check whether the main element already exists in the content.

Check out the implementation in the Group block.

@EliezerSPP
Copy link
Contributor Author

@t-hamano Thanks for the quick review and the helpful suggestion! I've updated the PR to use HTMLElementControl as you recommended. That's a much cleaner solution.

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

Thanks for the update! I'd recommend two more code quality enhancements.

For security improvements, it's a good idea to use the tag_escape() function, which is also done in the Template Part block. It should look something like this.

$tag_name = 'div';

if ( ! empty( $attributes['tagName'] ) && tag_escape( $attributes['tagName'] ) === $attributes['tagName'] ) {
	$tag_name = $attributes['tagName'];
}

Next, when generating output HTML, it is common to use the sprintf function in WordPress.

return sprintf(
	'<%1$s %2$s>%3$s</%1$s>',
	$tag_name,
	$wrapper_attributes,
	$content
);

@EliezerSPP
Copy link
Contributor Author

EliezerSPP commented Jul 15, 2025

@t-hamano These are excellent code quality enhancements.

I've implemented both, these improvements definitely make the code more robust.

Could you take another quick look when you have a moment? Thanks in advance!

Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@t-hamano t-hamano merged commit 6a7a0a9 into WordPress:trunk Jul 16, 2025
60 checks passed
@github-actions github-actions bot added this to the Gutenberg 21.3 milestone Jul 16, 2025
@EliezerSPP EliezerSPP deleted the patch-5 branch July 20, 2025 16:38
USERSATOSHI pushed a commit to USERSATOSHI/gutenberg that referenced this pull request Jul 23, 2025
* Post Content Block: Add tagName selector

* fix: Define $tag_name

* style: Format code

* docs: Update core-blocks.md

* Update post content block fixture

* docs: Update docs

* feat: Implement HTMLElementControl

* enhance: Better tagName handling for security

Co-authored-by: EliezerSPP <eliezerspp@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Content Affects the Post Content Block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants