Skip to main content

custom wrapper

<?php

/*
  Wrap Gutenberg blocks in a custom wrapper
  .block-is-wrapped is for general use     
 */
function gutenberg_block_wrapper( $block_content, $block ) {

  // No block name? Most likely doesn't exist
  if ( NULL === $block['blockName']  ) {
    return;
  }

  // We might need to steal the classname from the
  // element and apply it to the parent wrapper we're adding
  $blockClass = sanitize_title( $block['blockName'] );
  if ( isset( $block['attrs']['className'] ) ) {
    $blockClass = sanitize_title( $block['blockName'] ) . ' wrapped-' . $block['attrs']['className'];
  }

  // Wrap the element in a wrapper
  return sprintf(
    '<section class="block-is-wrapped block-%s">%s</section>',
    $blockClass,
    $block_content
  );
}

add_filter( 'render_block', 'gutenberg_block_wrapper', 10, 2 );