admin管理员组文章数量:1277362
I am extending a core WP block (core/group
), so that it has some added div's
enabling me to display the block as I desire on front end.
In addition to the display customization (which is not possible with style properties alone - hence the added markup), the point of my extending the block with this added markup is so that on front end I can do jQuery actions, based on an id
I am adding to my container div
, as well as create a dynamic navigation menu with anchor links to the blocks based on presence of my extended blocks.
First, let me say up front, everything is already working precisely how I want on front end, and in the block editor, everything seems to be working great, except for one validation issue, which doesn't seem to be a show stopper, but nonetheless, I want to get rid of the validation error.
I am noticing that Wordpress is automatically adding an id
to the core/group outer div: <div class="wp-block-group" id="an-exercise-111"></div>
. This is causing the validation error, and I can't figure out how to stop it from doing so.
In my code, I don't see anyplace where this id could be getting added.
Here is my code that generates the custom block markup:
const addContentTypeMarkup = ( element, blockType, attributes ) => {
// Do nothing if it's another block than our defined ones.
if ( ! enableBlockContentTypeAttribute.includes( blockType.name ) ) {
return element;
}
if ( attributes.contenttitle) {
var post_id = wp.data.select("core/editor").getCurrentPostId();
var title_slug = attributes.contenttitle.trim().split(/\s+/).join('-');
var anchorid = title_slug+'-'+attributes.contentid;
var iconclassbase='contenticon ';
var iconclass='';
switch(attributes.contenttype) {
case 'exercise':
iconclass = 'fas fa-guitar';
break;
case 'concept':
iconclass = 'fas fa-atom';
break;
default:
iconclass = 'fas fa-atom';
}
iconclass = iconclassbase + iconclass;
return (
<React.Fragment>
<div id = {`${anchorid}`} className = {`contenttype-wrapper sometopictype-${attributes.contenttype} navanchor`} data-id = {`${attributes.blockId}`}>
<div className = "heading d-flex flex-row">
<i className={`${iconclass} fa-7x`}></i>
<div className = "col">
<div className = "row"><span className="content-name">{attributes.contentname}</span></div>
<div className = "row"><h3 className="topictype-title" dangerouslySetInnerHTML={ { __html: attributes.contenttitle } }></h3></div>
</div>
</div>
{element}
</div>
</React.Fragment>
)
} else {
return element;
}
};
addFilter( 'blocks.getSaveElement', 'my-blockmods/add-content-type-markup', addContentTypeMarkup);
Here is the message I get in admin panel, if I view console in Developer Tools:
<div id="an-exercise-111" class="contenttype-wrapper sometopictype-exercise navanchor" data-id="be3d05d6-a048-41c9-8dcf-5fcf121dd4a8"><div class="heading d-flex flex-row"><i class="contenticon fas fa-guitar fa-7x"></i><div class="col"><div class="row"><span class="content-name">Exercise</span></div><div class="row"><h3 class="topictype-title">an exercise</h3></div></div></div><div class="wp-block-group" id="an-exercise-111"></div></div>
Content retrieved from post body:
<div id="an-exercise-111" class="contenttype-wrapper sometopictype-exercise navanchor" data-id="be3d05d6-a048-41c9-8dcf-5fcf121dd4a8"><div class="heading d-flex flex-row"><i class="contenticon fas fa-guitar fa-7x"></i><div class="col"><div class="row"><span class="content-name">Exercise</span></div><div class="row"><h3 class="topictype-title">an exercise</h3></div></div></div><div class="wp-block-group"></div></div>
I see the nature of the problem - Wordpress Block Editor, upon save
, is creating this element:
div class="wp-block-group" id="an-exercise-111"></div>
This element, shown above in the markup function simply as {element}
contains the content of the block. I don't need it to have an id
. Maybe Wordpress requires its blocks to have an id
? So it seems the block editor is automatically setting it with an id
that is the same as the id
I set manually in the block markup function, where I have <div id="an-exercise-111" class="contenttype-wrapper...
Any ideas?
thanks!
NOTE
If I don't use the id
attribute in my custom div
then everything validates. For example, if I use someid
then all is fine. But the whole point to me needing to use id
is so that anchor links work the way I want. So this gets back to original question - why is WP even adding the id
to the div <div class="wp-block-group" id="an-exercise-111">
? In block editor, I don't add an id to the core/group block, and in my filter, I don't set that id. Really, this is a mystery. Even if I could do this differently (and I will probably switch to render_block and do server side render), I would still like to understand why WP is doing what it is doing...
I am extending a core WP block (core/group
), so that it has some added div's
enabling me to display the block as I desire on front end.
In addition to the display customization (which is not possible with style properties alone - hence the added markup), the point of my extending the block with this added markup is so that on front end I can do jQuery actions, based on an id
I am adding to my container div
, as well as create a dynamic navigation menu with anchor links to the blocks based on presence of my extended blocks.
First, let me say up front, everything is already working precisely how I want on front end, and in the block editor, everything seems to be working great, except for one validation issue, which doesn't seem to be a show stopper, but nonetheless, I want to get rid of the validation error.
I am noticing that Wordpress is automatically adding an id
to the core/group outer div: <div class="wp-block-group" id="an-exercise-111"></div>
. This is causing the validation error, and I can't figure out how to stop it from doing so.
In my code, I don't see anyplace where this id could be getting added.
Here is my code that generates the custom block markup:
const addContentTypeMarkup = ( element, blockType, attributes ) => {
// Do nothing if it's another block than our defined ones.
if ( ! enableBlockContentTypeAttribute.includes( blockType.name ) ) {
return element;
}
if ( attributes.contenttitle) {
var post_id = wp.data.select("core/editor").getCurrentPostId();
var title_slug = attributes.contenttitle.trim().split(/\s+/).join('-');
var anchorid = title_slug+'-'+attributes.contentid;
var iconclassbase='contenticon ';
var iconclass='';
switch(attributes.contenttype) {
case 'exercise':
iconclass = 'fas fa-guitar';
break;
case 'concept':
iconclass = 'fas fa-atom';
break;
default:
iconclass = 'fas fa-atom';
}
iconclass = iconclassbase + iconclass;
return (
<React.Fragment>
<div id = {`${anchorid}`} className = {`contenttype-wrapper sometopictype-${attributes.contenttype} navanchor`} data-id = {`${attributes.blockId}`}>
<div className = "heading d-flex flex-row">
<i className={`${iconclass} fa-7x`}></i>
<div className = "col">
<div className = "row"><span className="content-name">{attributes.contentname}</span></div>
<div className = "row"><h3 className="topictype-title" dangerouslySetInnerHTML={ { __html: attributes.contenttitle } }></h3></div>
</div>
</div>
{element}
</div>
</React.Fragment>
)
} else {
return element;
}
};
addFilter( 'blocks.getSaveElement', 'my-blockmods/add-content-type-markup', addContentTypeMarkup);
Here is the message I get in admin panel, if I view console in Developer Tools:
<div id="an-exercise-111" class="contenttype-wrapper sometopictype-exercise navanchor" data-id="be3d05d6-a048-41c9-8dcf-5fcf121dd4a8"><div class="heading d-flex flex-row"><i class="contenticon fas fa-guitar fa-7x"></i><div class="col"><div class="row"><span class="content-name">Exercise</span></div><div class="row"><h3 class="topictype-title">an exercise</h3></div></div></div><div class="wp-block-group" id="an-exercise-111"></div></div>
Content retrieved from post body:
<div id="an-exercise-111" class="contenttype-wrapper sometopictype-exercise navanchor" data-id="be3d05d6-a048-41c9-8dcf-5fcf121dd4a8"><div class="heading d-flex flex-row"><i class="contenticon fas fa-guitar fa-7x"></i><div class="col"><div class="row"><span class="content-name">Exercise</span></div><div class="row"><h3 class="topictype-title">an exercise</h3></div></div></div><div class="wp-block-group"></div></div>
I see the nature of the problem - Wordpress Block Editor, upon save
, is creating this element:
div class="wp-block-group" id="an-exercise-111"></div>
This element, shown above in the markup function simply as {element}
contains the content of the block. I don't need it to have an id
. Maybe Wordpress requires its blocks to have an id
? So it seems the block editor is automatically setting it with an id
that is the same as the id
I set manually in the block markup function, where I have <div id="an-exercise-111" class="contenttype-wrapper...
Any ideas?
thanks!
NOTE
If I don't use the id
attribute in my custom div
then everything validates. For example, if I use someid
then all is fine. But the whole point to me needing to use id
is so that anchor links work the way I want. So this gets back to original question - why is WP even adding the id
to the div <div class="wp-block-group" id="an-exercise-111">
? In block editor, I don't add an id to the core/group block, and in my filter, I don't set that id. Really, this is a mystery. Even if I could do this differently (and I will probably switch to render_block and do server side render), I would still like to understand why WP is doing what it is doing...
1 Answer
Reset to default 1You don't, rather the approach taken is incorrect, and this particular situation is not fixable.
The group blocks anchor attribute for the ID is stored in the markup itself not the HTML comment, so by introducing an ID at the top level you are changing its value. Once this is done, the output of the save component of the group block changes and that is where your ID is coming from.
Fundamentally, what you're doing is not how it should be done, and this approach is the wrong approach.
Instead, you should use containing blocks and compose your structure. Much in the same way that you can group blocks together in a group block, and give it an ID, you would be better off creating a dedicated block with a title and icon selection that has an ID.
If a container block isn't suitable, a step heading block would also work just as well.
The bonus would be that you could build the navigation trivially in PHP by parsing the blocks, either through parse_blocks
then looping through to find these "step" blocks, or by adding a filter to the_content
, or even a template function if you wanted to do it that way. Likewise, any question about building outliners for block based content would also be super useful to you.
Likewise a filter similar to what you have right now in PHP could be implemented using the block rendering filters at runtime to achieve this.
本文标签: How to prevent Block Editor from adding id to block markup in save function
版权声明:本文标题:How to prevent Block Editor from adding id to block markup in save function? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741209465a2358816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
exercise
, block, which will have certain formatting. I place them on a custom post. So you can imagine a custom postguitar_lesson
and within that post you have exercises, etc. These exercises are not posts - they are just blocks. Upon save_post, I detect these blocks in post_content and update some post meta so that I keep track of these blocks for use in a nav menu. All works great on front end. Just this validation error - no idea how this id is being added. – Brian Commented Nov 23, 2021 at 15:31<div id = {
${anchorid}} className = {
contenttype-wrapper sometopictype-${attributes.contenttype} navanchor} content-id = {
${attributes.contentid}} data-id = {
${attributes.blockId}}>
I suppose I wasn't clear - the block editing is so that I have custom version of a core/group block, whose added markup allows me to do some jQuery actions on front end. Everything is working - I just can't figure out why WP adds to the block {element} the same id I add to my custom div. – Brian Commented Nov 24, 2021 at 8:06