Last active
July 14, 2022 09:41
-
-
Save plasticmind/1509d93f9dbcb3186332ee8dced5b265 to your computer and use it in GitHub Desktop.
Adding column count class to Gutenberg columns block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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