WordPress.org

Make WordPress Core

Opened 9 months ago

Last modified 9 months ago

#46187 new enhancement

Add a dynamic hook to filter the content of a single block.

Reported by: manzoorwani.jk Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Editor Keywords: has-patch
Focuses: Cc:
PR Number:

Description

render_block filter in render_block() is very helpful. It would be good to have a dynamic hook (render_block_{$block_name}) to filter only the content of a specific block, to avoid the need of using the conditionals inside the filter callback.

register_block_type() does provide an option to pass render_callback but still that's not useful when you are not the creator of the block.

Attachments (2)

46187.diff (868 bytes) - added by manzoorwani.jk 9 months ago.
46187-1.diff (1.5 KB) - added by manzoorwani.jk 9 months ago.

Download all attachments as: .zip

Change History (5)

#1 @birgire
9 months ago

  • Keywords has-patch added

Thanks for the ticket @manzoorwanijk

At a first glance this sounds useful.

Using a dynamic filter, we should make sure that the block names can't contain characters that are not supported as filter names.

Few questions come to mind: What limitations are on the block names and filter names? For example what is the maximum length of filter names and block names? Is it the same?

Regarding the if-condition in 46187.diff, should e.g. "0" be allowed, that is if "0" is a possible block name?

If this goes through, then it would need a @since x.y.z for the the filter documentation.

There seems to be some code standard adjustments needed in 46187.diff :

  • I think the variable assigning within the if-condition is not recommended.
  • Missing end of line periods in inline documentation.
  • Fix indentation.
  • First word in inline documentation not capitalized.

Should the specific dynamic filter come before or after the existing and more general render_block filter? I didn't find quickly some good existing core examples for the case of filters, but we have e.g. the dynamic action "save_post_{$post->post_type}" running before "save_post". In that case the more specific comes before the general one. This is the same order as in 46187.diff.

#2 follow-up: @manzoorwani.jk
9 months ago

Thank you for the valuable inputs @birgire

I have made the changes pointed out by you and also moved the dynamic filter after the general filter which seems to be a better place for a dynamic filter unlike hooks, couldn't find an example from the core.

Also, about the number of characters in a filter tag, I found your own answer 🙂

As far as the allowed characters in a filter are concerned, WordPress is stricter when it comes to a block name, so it should be pretty fine to use a block name in a dynamic filter, provided / between namespace and block is not a problem.

#3 in reply to: ↑ 2 @birgire
9 months ago

Replying to manzoorwani.jk:

lol, thanks for the link 🙂

Note: See TracTickets for help on using tickets.