Skip to content

Instantly share code, notes, and snippets.

@plasticmind
Last active July 14, 2022 09:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plasticmind/1509d93f9dbcb3186332ee8dced5b265 to your computer and use it in GitHub Desktop.
Save plasticmind/1509d93f9dbcb3186332ee8dced5b265 to your computer and use it in GitHub Desktop.
Adding column count class to Gutenberg columns block
<?php
/**
* inject_class_column_count
*
* @param string $content The block content about to be appended.
* @param array $block The full block, including name and attributes
* @return $content;
*/
function inject_class_column_count( $content, $block ) {
if ( ! is_block_type( $block, "core/columns" ) ) {
return $content;
} else {
$column_count = array_column($block['innerBlocks'],'blockName');
$modified_content = str_replace('wp-block-columns','wp-block-columns has-'.count($column_count).'-columns',$content);
return $modified_content;
}
}
add_filter( 'render_block', 'inject_class_column_count', 10, 2 );
/**
* is_block_type
*
* @param array $block A WordPress block array
* @param string $type The block name being queried
* @return bool;
*/
function is_block_type( $block, $type ) {
if ( $type === $block['blockName'] ) {
return true;
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment