Skip to content

Instantly share code, notes, and snippets.

@plasticmind

plasticmind/functions.php

Last active Feb 20, 2020
Embed
What would you like to do?
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