I use a block filter to add classes to core and custom blocks. It doesn't work with dynamic blocks though. I tested with my custom block, and then tried with the core latest posts block and it didn't work either. Is it not possible to use block filters to add classes to dynamic blocks?

I'm using the blocks.getSaveContent.extraProps filter, which adds a class to the block on the frontend. My php template for the dynamic block uses this as the outer wrapper, which pulls in alignment and color classes, just not block filter classes:

<div <?php echo wp_kses_data( get_block_wrapper_attributes() ); ?>> inner block stuff</div>

  • Were you able to solve this? I'm also trying to modify the core/post-template but it does not get called on the hook. Nov 11, 2022 at 21:39
  • 1
    @GilsonViana Unfortunately, I was not able to solve it. When I do (hopefully), I'll add the solution.
    – Elizabeth
    Nov 15, 2022 at 19:48
  • my workaround solution for this was to wrap my dynamic block with a core/group block. I found out that is also possible to modify the core block using the following filter register_block_type_args. Nov 15, 2022 at 20:05

1 Answer 1


I had the same problem, and stumbled upon this in-depth answer in the closed issue on the Gutenberg repo:


I ended up adding the class using the render_block filter in PHP, and it is working for me (slightly tweaked from the original example, hopefully for the better):

function add_custom_block_class( $block_content, $block ) {
    if ( 
        isset( $block['attrs']['imageHoverEffectEnabled'] ) && 
        $block['attrs']['imageHoverEffectEnabled'] === true 
        ) {
            $injected_class = 'hover-effect-enabled';
            return preg_replace(
                '/' . preg_quote( 'class="', '/' ) . '/',
                'class="' . esc_attr( $injected_class ) . ' ',
    }else {
        return $block_content;
add_filter( 'render_block_core/post-featured-image', 'add_custom_block_class', 10, 2 );

Note: I'm using the block specific filter render_block_core/post-featured-image instead of render_block as this is what I needed in my case.

The $block['attrs']['imageHoverEffectEnabled'] value comes from a ToggleControl component from my custom block.

Hope that helps someone.

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.