Skip to content

Instantly share code, notes, and snippets.

@gcordner
Forked from bacoords/columns-block-filter.php
Created January 16, 2024 23:17
Show Gist options
  • Save gcordner/bfe922da1a9bfe52d465d44fa169ccbd to your computer and use it in GitHub Desktop.
Save gcordner/bfe922da1a9bfe52d465d44fa169ccbd to your computer and use it in GitHub Desktop.
<?php
/**
* Render columns block with extra class
*
* @param string $block_content The block content about to be rendered.
* @param array $block The full block, including name and attributes.
* @return string
*/
function render_columns_block_class( $block_content, $block ) {
$columns = count( $block['innerBlocks'] );
$block_content = str_replace( 'wp-block-columns', 'wp-block-columns wp-block-columns-has-' . $columns, $block_content );
return $block_content;
}
add_filter( 'render_block_core/columns', 'render_columns_block_class', 10, 2 );
@gcordner
Copy link
Author

This offered in response to this tweet by carrie dils: https://twitter.com/briancoords/status/1729589304593162558

For Christmas, I wish there was an auto-class applied to the Columns block indicating the number of child elements. Sure would be nicer than relying on content editors to add a custom CSS class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment