Make WordPress Core

Opened 8 months ago

Last modified 11 hours ago

#60544 new enhancement

Allow individual blocks to be excluded from `excerpt_remove_blocks()`

Reported by: jeremyfelt's profile jeremyfelt Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.5
Component: Formatting Keywords: has-patch
Focuses: Cc:

Description

In excerpt_remove_blocks(), it is possible to to filter the list of block names that should be included when generating excerpt text.

It should also be possible to selectively exclude (or include) individual blocks based on their individual attributes.

For this to work, a new filter would need to be inserted in excerpt_remove_blocks() and _excerpt_render_inner_blocks().

I think it would then be enough to check with the existing logic as:

if (
    in_array( $block['blockName'], $allowed_blocks, true )
    && apply_filters( 'excerpt_allowed_block', true, $block )
)

Then, if I added a custom attribute to a core (e.g. paragraph) block, I could add a filter to return false when that attribute was detected.

The current alternative for this is unhooking wp_trim_excerpt() from get_the_excerpt() entirely and then reusing it as forked code with an alternate version of excerpt_remove_blocks().

Change History (1)

This ticket was mentioned in PR #7545 on WordPress/wordpress-develop by @debarghyabanerjee.


11 hours ago
#1

  • Keywords has-patch added; needs-patch removed

Trac Ticket: Core-60544

## Overview

  • This pull request enhances the excerpt_remove_blocks() and _excerpt_render_inner_blocks() function by introducing a new filter, excerpt_allowed_block, which allows developers to control whether individual blocks should be included in post excerpts based on custom criteria.

## Problem Statement

  • The current implementation of excerpt_remove_blocks() relies on a fixed list of allowed blocks, limiting the ability to customize excerpts according to specific needs. Developers may want to exclude blocks based on their attributes, but doing so requires forking core functionality, which can lead to maintenance issues.

## Proposed Solution

  • Addition of excerpt_allowed_block Filter:
  • This new filter enables developers to modify the inclusion criteria for individual blocks within excerpts. By allowing a boolean return value based on custom logic, developers can easily control which blocks are allowed.

Benefits

  • Increased Flexibility:
  • Developers can now exclude specific blocks based on custom attributes or other criteria without modifying core functions.
  • Enhanced Extensibility:
    • The introduction of the filter supports easier integration with themes and plugins.
  • Improved Maintainability:
    • Cleaner code structure and the reduction of fixed logic help facilitate future updates.
Note: See TracTickets for help on using tickets.