-
-
Save chrisvanpatten/ca93abd76e62b01cdf598ee9a9bcbd08 to your computer and use it in GitHub Desktop.
Wrap alignwide/alignfull Gutenberg blocks
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 | |
/** | |
* Make sure you have QueryPath installed and loaded via Composer | |
*/ | |
/** | |
* Filter the_content to fix/improve column markup | |
* | |
* @param string $content | |
* | |
* @return string | |
*/ | |
function wrapWideBlocks($content) { | |
// If the post does not contain alignwide or alignfull, return early | |
if (strpos($content, 'alignwide') === false && strpos($content, 'alignfull') === false) { | |
return $content; | |
} | |
// Load the content | |
$qp = html5qp("<!DOCTYPE html><html><body>{$content}</body></html>", 'body'); | |
// Find all the aligned blocks | |
$blocks = $qp->find('.alignwide, .alignfull'); | |
// Add two wrappers on each aligned block | |
foreach ( $blocks as $block ) { | |
$block | |
->wrap('<div class="tomodomo-align-container"></div>') | |
->wrap('<div class="container"></div>'); | |
} | |
// Return the modified post content | |
return $qp->find('body')->html5(); | |
} | |
add_filter('the_content', 'wrapWideBlocks', 9); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment