I'm trying to add data attributes to elements in a block theme, not blocks themselves. It appears WordPress strips them out by default.
I've tried in the theme template parts and in patterns:
<article data-attribute-etc="value" class="etc"></article>
I'm unsure if I have to, or even can, first affect KSES to allow the specific data attributes I'm interested in and then add them to the theme. Any pointers would be useful, thanks.
I'm trying to add data attributes to elements in a block theme, not blocks themselves. It appears WordPress strips them out by default.
I've tried in the theme template parts and in patterns:
<article data-attribute-etc="value" class="etc"></article>
I'm unsure if I have to, or even can, first affect KSES to allow the specific data attributes I'm interested in and then add them to the theme. Any pointers would be useful, thanks.
If the element is not in a block then it can be added directly with HTML.
example :
<article data-attribute-etc="value" class="etc">
<!-- wp:group {"align":"full","layout":{"type":"constrained"}} -->
<div class="wp-block-group alignfull">
<!-- wp:post-title /-->
</div>
<!-- /wp:group -->
</article>
If the element is in a block then use block custom html
example :
<!-- wp:group {"align":"full","layout":{"type":"constrained"}} -->
<div class="wp-block-group alignfull">
<!-- wp:html -->
<article data-attribute-etc="value" class="etc">
<!-- /wp:html -->
<!-- wp:post-title /-->
<!-- wp:html -->
</article>
<!-- /wp:html -->
</div>
<!-- /wp:group -->
wp_kses_post
doesn't strip out data attributes, so that isn't the problem, though as I said in my previous comment it's not stripping out, it's overwriting the entire blocks HTML with fresh new HTML, no modifying is happening.'<article data-attribute-etc="value" class="etc"></article>' === wp_kses_post( '<article data-attribute-etc="value" class="etc"></article>' )
– Tom J Nowell ♦ Commented May 21 at 19:37.html
files as post content as it's stored in the database, not raw HTML to be edited like any other HTML file – Tom J Nowell ♦ Commented May 22 at 21:05