Skip to content

Instantly share code, notes, and snippets.

@chrisvanpatten
Last active January 11, 2021 22:51
Show Gist options
  • Save chrisvanpatten/ca93abd76e62b01cdf598ee9a9bcbd08 to your computer and use it in GitHub Desktop.
Save chrisvanpatten/ca93abd76e62b01cdf598ee9a9bcbd08 to your computer and use it in GitHub Desktop.
Wrap alignwide/alignfull Gutenberg blocks
<?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