Remove headings or any blocks from post excerpts

It’s possible to remove any block types from the WordPress post excerpts. These excerpts usually appear on archieve pages and can look weird due to block content.

We’ve put together an example that will remove all Gutenberg heading blocks (h1, h2, h3, h4, h5, h6) from the post excerpt.

// Remove heading blocks from excerpts. function custom_remove_headings_blocks_excerpts( $allowed_blocks ) { $key = array_search( 'core/heading', $allowed_blocks ); if ( $key !== false ) { unset( $allowed_blocks[ $key ] ); } return $allowed_blocks; } add_filter( 'excerpt_allowed_blocks', 'custom_remove_headings_blocks_excerpts', 100, 1 );
Code language: PHP (php)

The above function removes the core/heading block from the allowed blocks in the excerpts.

To further customize the excerpt with blocks, you should have a look at the wp_trim_excerpt(); and excerpt_remove_block(); functions. These functions handle the excerpt output. You can also add any third-party blocks to the excerpt_allowed_blocks filter by adding the block name into the $allowed_blocks array.

It’s worth noting that dynamic and nested blocks are removed by default from the post excerpt.

You can remove any of the default Gutenberg blocks by following our above example.

  • core/freeform
  • core/heading
  • core/html
  • core/list
  • core/media-text
  • core/paragraph
  • core/preformatted
  • core/pullquote
  • core/quote
  • core/table
  • core/verse

Hi, I'm Morgan 👋

I make things happen unconditionally here at Conditional Blocks. I believe that WordPress Blocks are the future.

Got a conditional idea to make Gutenberg even more powerful? Ping me!