Extend core block attributes in post content

I need help with adding attribute ‘caption’ of core/image block into saved post content.

Currently saved attributes look like follow:

<!-- wp:image {"id":36,"sizeSlug":"large"} -->

I aim to it looks like this:

<!-- wp:image {"id":36,"sizeSlug":"large","caption":"my caption"} -->

So I tried to use wp filter blocks.getSaveElement:
https://developer.wordpress.org/block-editor/developers/filters/block-filters/#blocks-getsaveelement

function test(element, blockType, attributes) {
    if (blockType.name !== 'core/image') {
        return element;
    }

    console.log(attributes.caption); // here is my attribute available

    return element;
}

wp.hooks.addFilter('blocks.getSaveElement', 'whatever', test);

Unfortunately I still can not figure out, how to save my attribute into post content. Any help please?

0

Leave a Comment