$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>customization - Add a containing DIV to core Gutenberg blocks|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

customization - Add a containing DIV to core Gutenberg blocks

matteradmin11PV0评论

The design I am working with calls for each Gutenberg block to have a coloured background (white in this case) with max-width: 1160px and the content within to have max-width: 800px and centered. Similar to the image below:

Which would need a setup like this:

<main class="site-background">
    <div class="width-1160">
        <p class="width-800">Some text content...</p>
    </div>
</main>

At this time we are only using core Gutenberg blocks but the markup it produces is similar to the below, without the containing <div>:

<main class="site-background">
    <p class="width-800">Some text content...</p>
</main>

Is there a way to add a containing <div> to all Gutenberg content blocks that will be safe when core updates?

The design I am working with calls for each Gutenberg block to have a coloured background (white in this case) with max-width: 1160px and the content within to have max-width: 800px and centered. Similar to the image below:

Which would need a setup like this:

<main class="site-background">
    <div class="width-1160">
        <p class="width-800">Some text content...</p>
    </div>
</main>

At this time we are only using core Gutenberg blocks but the markup it produces is similar to the below, without the containing <div>:

<main class="site-background">
    <p class="width-800">Some text content...</p>
</main>

Is there a way to add a containing <div> to all Gutenberg content blocks that will be safe when core updates?

Share Improve this question asked Feb 22, 2019 at 11:17 BagseyeBagseye 691 silver badge12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

I'm sure there are other ways e.g. with CSS only or Add custom class to core blocks in Gutenberg, but regarding:

Add a containing DIV to core Gutenberg blocks

one way could be with the render_block filter:

add_filter( 'render_block', function( $block_content, $block ) {
    // Target core/* and core-embed/* blocks.
    if ( preg_match( '~^core/|core-embed/~', $block['blockName'] ) ) {
       $block_content = sprintf( '<div class="some__class">%s</div>', $block_content );
    }
    return $block_content;
}, PHP_INT_MAX - 1, 2 );

to div-wrap the core blocks on the frontend.

Changing the HTML layout might affect the current style of the site.

Post a comment

comment list (0)

  1. No comments so far