最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

customization - When using the block.getSaveElement hook can you output different markup based on whether or not you are in the

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far