Skip to content

Instantly share code, notes, and snippets.

@adamf321
Last active January 12, 2021 19:20
Show Gist options
  • Save adamf321/02e51a0d2f8af4243476c34bf4037703 to your computer and use it in GitHub Desktop.
Save adamf321/02e51a0d2f8af4243476c34bf4037703 to your computer and use it in GitHub Desktop.
<?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