#59424 closed enhancement (fixed)
Blocks: Introduce filter to allow easy addition of hooked blocks
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.4 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | General | Keywords: | has-patch has-unit-tests commit |
| Focuses: | Cc: |
Description (last modified by )
As a follow-up to [56649], it would be good to introduce a filter that allows even easier conditional addition (or removal) of hooked blocks for a given anchor block and relative position.
Something along the following lines:
<?php function insert_shopping_cart_hooked_block( $hooked_blocks, $position, $anchor_block, $context ) { if ( 'after' === $position && 'core/navigation' === $anchor_block && /** $context is header template part **/ ) { $hooked_blocks[] = 'mycommerce/shopping-cart'; } return $hooked_blocks; } add_filter( 'hooked_block_types', 'insert_shopping_cart_hooked_block', 10, 4 );
Change History (10)
This ticket was mentioned in PR #5271 on WordPress/wordpress-develop by @Bernhard Reiter.
2 years ago
#1
- Keywords has-patch has-unit-tests added
@Bernhard Reiter commented on PR #5271:
2 years ago
#2
FYI @nerrad @ndiego
#5
@
2 years ago
I left my feedback on the PR. I like the proposal very much and I would be in favor of replacing existing low-level filters with hooked_block_types.
@Bernhard Reiter commented on PR #5271:
2 years ago
#6
Excellent idea with the new better scoped filters
hooked_block_types. I would be in favor of removinginject_hooked_block_markupfilters as they are low-level and offer too much power in contrast to the new proposal. We can always add them back later if it turns out they are essential for more advanced usage that I can't think of at the moment.
Great, thank you! I agree 100% -- YAGNI and all 😉
I'll land this change, and will then do a follow-up to remove inject_hooked_block_markup.
@Bernhard Reiter commented on PR #5271:
2 years ago
#9
Committed to Core in https://core.trac.wordpress.org/changeset/56673.
Introduce a
hooked_block_typesfilter that allows even easier conditional addition (or removal) of hooked blocks for a given anchor block and relative position.function insert_shopping_cart_hooked_block( $hooked_blocks, $position, $anchor_block, $context ) { if ( 'after' === $position && 'core/navigation' && $anchor_block && /** $context is header template part **/ ) { $hooked_blocks[] = 'mycommerce/shopping-cart'; } return $hooked_blocks; } add_filter( 'hooked_block_types', 'insert_shopping_cart_hooked_block', 10, 4 );Trac ticket: https://core.trac.wordpress.org/ticket/59424