Created
October 13, 2023 07:54
-
-
Save MatzeKitt/65053f0d083bec0f49d21d1626d2ef38 to your computer and use it in GitHub Desktop.
Add has-\d-columns class to columns blocks after WordPress 5.3 update.
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 | |
/** | |
* Add has-\d-columns class to columns blocks after 5.3 update. | |
* | |
* @param string $block_content The block content about to be appended | |
* @param array $block The full block, including name and attributes | |
* @return string The updated block content | |
*/ | |
add_filter( 'render_block_core/columns', function( $block_content, $block ) { | |
$columns_count = count( $block['innerBlocks'] ); | |
// Check if the columns block already has a has-\d-columns class. | |
if ( preg_match( '/<div class="(?:.* )?(wp-block-columns (?:.*)has-' . $columns_count . '-columns|has-' . $columns_count . '-columns (?:.*)wp-block-columns)(?:.*)?"(?:.*)>/', $block_content ) ) { | |
return $block_content; | |
} | |
// Add the class to the markup. | |
$block_content = preg_replace( '/<div class="(.* )?wp-block-columns((?:.* ?)?)"(.*)>/', sprintf( | |
'<div class="$1wp-block-columns has-%d-columns $2"$3>', | |
$columns_count | |
), $block_content, 1 ); | |
return $block_content; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment