lightbulbheaduk/wordpress

Wordpress 6.3 removes html tags from custom excerpt

Closed this issue ยท 6 comments

Upgrade from 6.2 to 6.3 removes the html tags in the output of code the replaces the excerpt

OK, so it looks like WordPress/Documentation-Issue-Tracker#687 changes were merged into 6.3 to introduce excerpt length controls within UI.

The pull request WordPress/gutenberg#44964 adds logic to packages/block-library/src/post-excerpt/index.php to check if an excerpt_length exists... if it does, then it calls "wp_trim_words()" which strips all HTML tags

$excerpt = wp_trim_words( get_the_excerpt(), $excerpt_length );

Looks like we'll need to over-ride wp_trim_words...?

There doesn't seem to be anyway to override the HTML stripping taking place with the query loop excerpt block

Yep, I agree, which is why I also made a comment on the Gutenberg report to that effect. I haven't found a sensible workaround, so I believe the right thing is for it to be fixed in the main repo

Hi! Coming from a different issue I faced with WordPress's new way of handling excerpts.

I found that the "block_type_metadata" filter gives me ability to override original Gutenberg block settings and this way I can modify the original "excerptLength" config of the excerpt block:

function fzs_filter_metadata_registration( $metadata ) {
    if ($metadata['name'] === 'core/post-excerpt') {
        $metadata['attributes']['excerptLength'] = [
            'type' => 'number',
            'default' => 999, //any number
        ];
    }
    return $metadata;
};
add_filter( 'block_type_metadata', 'fzs_filter_metadata_registration' );

Although this does not help you with your problem directly, it is possible to completely remove the "excerptLength" key from the metadata attributes array, and hopefully this will help you too by using simply get_the_excerpt() again and not trimming it with wp_trim_words:

function fzs_filter_metadata_registration( $metadata ) {
    if ($metadata['name'] === 'core/post-excerpt') {
        unset($metadata['attributes']['excerptLength']);
    }
    return $metadata;
};
add_filter( 'block_type_metadata', 'fzs_filter_metadata_registration' );

@frzsombor that fixed my specific issue as well, thank you!

Fixed with solution from @frzsombor