Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function lean_column_props( string $block_content, array $block ) : string {
$width = $block['attrs']['width'] ?? false;
$class_pos = strpos( $block_content, 'wp-block-column' );
$column_class = 'w-full px-1';
$column_class .= ( $width ? ' md:w-' . round( 12 * $width / 100 ) . '/12 ' : '' );
// Add to the existing classes if there are some already.
$class_pos = strpos( $block_content, 'class="' );
if ( false !== $class_pos && $class_pos < strpos( $block_content, '>' ) ) {
return substr_replace( $block_content, 'class="' . $column_class . ' ', $class_pos, 7 );
}
// Or add a class property if one does not exist already.
$close_pos = strpos( $block_content, '>' );
if ( false !== $close_pos ) {
return substr_replace( $block_content, ' class="' . $column_class . '">', $close_pos, 1 );
}
return $block_content;
}
<?php
function lean_columns_props( string $block_content, array $block ) : string {
$columns_class = "md:flex -mx-1";
// Add to the existing classes if there are some already.
$class_pos = strpos( $block_content, 'class="' );
if ( false !== $class_pos && $class_pos < strpos( $block_content, '>' ) ) {
return substr_replace( $block_content, 'class="' . $columns_class . ' ', $class_pos, 7 );
}
// Or add a class property if one does not exist already.
$close_pos = strpos( $block_content, '>' );
if ( false !== $close_pos ) {
return substr_replace( $block_content, ' class="' . $columns_class . '">', $close_pos, 1 );
}
return $block_content;
}
<?php
add_filter(
'render_block',
function( string $block_content, array $block ) {
if ( 'core/columns' === $block['blockName'] ) {
$block_content = lean_columns_props( $block_content, $block );
}
if ( 'core/column' === $block['blockName'] ) {
$block_content = lean_column_props( $block_content, $block );
}
return $block_content;
},
10,
3
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.