admin管理员组文章数量:1026205
I am trying to extend the core/image block to add an option for a deferred image. I want to replace the src attribute of the image with an empty string and add the url as a data attribute to be loaded later.
I can use the "url" attribute to populate the data attribute in the html, however if I delete the src attribute then wordpress thinks I need to pick another image puts in the image selection inspector control. I'd like to be able to still see what image I selected in the backend but remove the src attribute in the frontend. Anyone know if this is possible?
Here is some code for reference
const addDeferredPropToImage = (element, blockType, attributes) => {
if (blockType.name !== 'core/image') {
return element;
}
if (attributes.deferred) {
element.props.children.props.children.props.children[0].props['data-src'] = attributes.url;
}
return element;
}
wp.hooks.addFilter( 'blocks.getSaveElement', 'uwkc/get-save-content/get-save-element', addDeferredPropToImage );
If I try to remove the src attribute in that if clause that is when wordpress thinks I need to select a new image. It is there that I would like to also check whether I am in the edit screen or not.
I am trying to extend the core/image block to add an option for a deferred image. I want to replace the src attribute of the image with an empty string and add the url as a data attribute to be loaded later.
I can use the "url" attribute to populate the data attribute in the html, however if I delete the src attribute then wordpress thinks I need to pick another image puts in the image selection inspector control. I'd like to be able to still see what image I selected in the backend but remove the src attribute in the frontend. Anyone know if this is possible?
Here is some code for reference
const addDeferredPropToImage = (element, blockType, attributes) => {
if (blockType.name !== 'core/image') {
return element;
}
if (attributes.deferred) {
element.props.children.props.children.props.children[0].props['data-src'] = attributes.url;
}
return element;
}
wp.hooks.addFilter( 'blocks.getSaveElement', 'uwkc/get-save-content/get-save-element', addDeferredPropToImage );
If I try to remove the src attribute in that if clause that is when wordpress thinks I need to select a new image. It is there that I would like to also check whether I am in the edit screen or not.
本文标签:
版权声明:本文标题:customization - When using the block.getSaveElement hook can you output different markup based on whether or not you are in the 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745634748a2160377.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论