Filter parsed blocks into the_excerpt()

1 Reply ·

  1. Hi Justin,

    Since Gutenberg, my post excerpts include URLs when posts are starting with an embedded block such as youtube. I have tried to filter out these embedded blocks from parsing into the_excerpt() using the excerpt_allowed_blocks filter without any success. From my understanding, I would have to filter out core/embed and/or core-embed/youtube blocks but I can’t figure out how the filter works.

    Here is an example of what I have tried up now :

    add_filter( 'excerpt_allowed_blocks', 'rc_restrict_excerpt_to_paragraph_block' );
    function rc_restrict_excerpt_to_paragraph_block ( $allowed_blocks ) {
        $allowed_blocks = array(
            'core/paragraph',
        );
    return $allowed_blocks;
    }
    

    Your help would be appreciated.

    Regards.

    Patrick

  2. Justin Tadlock

    Your code looks fine.

    All embeds are excluded by default from excerpts in WordPress. I tested a YouTube embed myself to confirm this worked as expected. I don’t wonder if you have a plugin that’s changing it.

    For reference, here are the core blocks that are allowed by default:

    $allowed_inner_blocks = array(
        null, // classic block
        'core/freeform',
        'core/heading',
        'core/html',
        'core/list',
        'core/media-text',
        'core/paragraph',
        'core/preformatted',
        'core/pullquote',
        'core/quote',
        'core/table',
        'core/verse',
    );
    
    $allowed_blocks = array_merge( $allowed_inner_blocks, array( 'core/columns' ) );