Skip to content

Conversation

@khromov
Copy link

@khromov khromov commented Apr 3, 2024

👋 After using the plugin for a headless project I found myself in need to extend the block data.

For example, I needed to add the image dimensions to a core/image block. The width/height isn't present in the attrs array unless the user enters it using Gutenberg manually. With the new hook rest_api_blocks_handle_do_block you can prefill this data from the WordPress database if the user hasn't set it:

add_filter('rest_api_blocks_handle_do_block', function($block) {
    if($block['blockName'] === 'core/image') {
        $metadata = wp_get_attachment_metadata($block['attrs']['id']);

        if(!($block['attrs']['width'] ?? false) && $metadata['width']) {
            $block['attrs']['width'] = $metadata['width'] . 'px';
        }

        if(!($block['attrs']['height'] ?? false) && $metadata['height']) {
            $block['attrs']['height'] = $metadata['height'] . 'px';
        }
    }

    return $block;
});

Also added a couple of more hooks for completion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant