0

In gutenberg I have several blocks:

paragraph
heading
paragraph
code
paragraph

In php I'd like to find the code block and append content after the block.

I'd imagine I need to use add_filter and the_content, but I'm not sure how to go about it after that.

1 Answer 1

5

Yes, add_filter() is what you're looking for, but to identify a specific block, you want the render_block hook.

<?php
add_filter('render_block', function($block_content, $block) {
    // Only for Core Code blocks
    if('core/code' === $block['blockName']) {
        // Add your custom HTML after the block
        $block_content = $block_content . '<div>Test addition</div>';
    }
    // Always return the content
    return $block_content;
}, 10, 2);
?>

Anywhere the code block is rendered (which would currently be the front end of a Page, Post, or CPT, but in the future could be elsewhere) your added code will appear.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.