Last active
June 21, 2024 22:25
-
-
Save colorful-tones/b151d718dd2efce1469ccc302fe1d44e to your computer and use it in GitHub Desktop.
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
<!-- CURRENT block markup --> | |
<div class="wp-block-group cards"> | |
<div class="wp-block-cover"> | |
</div><!-- .wp-block-cover --> | |
</div><!-- .wp-block-group --> | |
<!-- DESIRED block markup --> | |
<ul class="wp-block-group cards"> | |
<li class="wp-block-cover card"></li> | |
</ul> |
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 | |
function cards_filter_group_content( $block_content ) { | |
$processor = new WP_HTML_Tag_Processor( $block_content ); | |
// Check for the presence of the 'cards' class. | |
if ( ! $processor->next_tag( array( 'class_name' => 'cards' ) ) ) { | |
return $block_content; | |
} | |
// Loop through each child block with the class name 'wp-block-cover'. | |
while ( $processor->next_tag( array( 'class_name' => 'wp-block-cover' ) ) ) { | |
$processor->add_class( 'card' ); | |
} | |
$block_content = $processor->get_updated_html(); | |
// Enqueue the custom script and style. | |
wp_enqueue_style( 'cards-style' ); | |
// Return the maybe modified block content. | |
return $block_content; | |
} | |
add_filter( 'render_block_core/group', 'cards_filter_group_content', 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment