Skip to content

Instantly share code, notes, and snippets.

@Mte90
Created June 11, 2020 15:51
Show Gist options
  • Save Mte90/077dcc444d4f8fbd996513c209563cad to your computer and use it in GitHub Desktop.
Save Mte90/077dcc444d4f8fbd996513c209563cad to your computer and use it in GitHub Desktop.
Remove WordPress Gutenberg block in content
<?php
function remove_block_gutenberg( $content, $blockname ) {
$blockname = str_replace('/', '\/',preg_quote($blockname));
return preg_replace('/\<\!\-\- ' . $blockname . '[^)]+\/' . $blockname . ' \-\-\>/', '', $content);
}
// Based on this ticket https://wordpress.org/support/topic/strip-table-of-content-from-excerpt/
// There is a block that we don't want on excerpt automatically generated,
// Automatic excerpt cut the content and this can generate a broken content
// This happens because blocks can be everything from html, json or html comments and they are in the post_content
// If you want to know more about the wrong technicial decisions in Gutenberg
// * https://daniele.tech/2020/05/gutenberg-drama-2020-edition-why-wordpress-is-against-the-wp-org-users/
// * https://daniele.tech/2019/05/your-wordpress-instance-is-leaking-data-also-with-gutenberg/
$content = remove_block_gutenberg(get_the_content(), 'wp:uagb/table-of-contents');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment