When registering new blocks, on front-end I get only the markup that I specify inside of save
method. Cool! That's what I need.
But when using the edit
method I get my markup wrapped in a div
. That's not what I need, because it limits my possibilities of styling.
How can I get rid of that div and have it similar to what the current core/paragraph block does, for example, with the p
tag?
Here is how I have it now:
and that's how it should become:
For reference the code is simple like this:
edit: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},
save: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},
When registering new blocks, on front-end I get only the markup that I specify inside of save
method. Cool! That's what I need.
But when using the edit
method I get my markup wrapped in a div
. That's not what I need, because it limits my possibilities of styling.
How can I get rid of that div and have it similar to what the current core/paragraph block does, for example, with the p
tag?
Here is how I have it now:
and that's how it should become:
For reference the code is simple like this:
edit: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},
save: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},
Share
Improve this question
asked Nov 20, 2020 at 10:41
Andrei SurduAndrei Surdu
6491 gold badge7 silver badges18 bronze badges
1 Answer
Reset to default 0Update: WordPress 5.6 will be released soon and this will include a block API version 2 which allows all these modifications noted in OP. https://make.wordpress/core/2020/11/18/block-api-version-2/
I found a filter that allows me to modify the wrapper. Still, I can't change the HTML tag, but it's better than nothing.
const { createHigherOrderComponent } = wppose;
const withClientIdClassName = createHigherOrderComponent( ( BlockListBlock ) => {
return ( props ) => {
const newClassName = "block-" + props.clientId + ' sample-block';
return <BlockListBlock { ...props } className={ newClassName } />;
};
}, 'withClientIdClassName' );
wp.hooks.addFilter( 'editor.BlockListBlock', 'awps/modify-blocks-wrapper', withClientIdClassName );
And the methods:
edit: () => {
return (
<mark>Sample</mark>
);
},
save: () => {
return (
<span className="sample-block">
<mark>Sample</mark>
</span>
);
},