Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit wordpress.stackexchange
scroll top