Make WordPress Core

Opened 7 months ago

Closed 7 months ago

Last modified 6 months ago

#59424 closed enhancement (fixed)

Blocks: Introduce filter to allow easy addition of hooked blocks

Reported by: bernhard-reiter's profile Bernhard Reiter Owned by: bernhard-reiter's profile Bernhard Reiter
Milestone: 6.4 Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description (last modified by Bernhard Reiter)

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.


7 months ago
#1

  • Keywords has-patch has-unit-tests added

Introduce a hooked_block_types filter 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

@Bernhard Reiter commented on PR #5271:


7 months ago
#2

FYI @nerrad @ndiego

#3 @Bernhard Reiter
7 months ago

  • Description modified (diff)

#4 @Bernhard Reiter
7 months ago

  • Type changed from defect (bug) to enhancement

#5 @gziolo
7 months 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:


7 months ago
#6

Excellent idea with the new better scoped filters hooked_block_types. I would be in favor of removing inject_hooked_block_markup filters 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.

#7 @Bernhard Reiter
7 months ago

  • Keywords commit added

#8 @Bernhard Reiter
7 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 56673:

Blocks: Introduce filter to allow easy addition of hooked blocks.

Introduce a hooked_block_types filter that allows 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 );

Props gziolo, nerrad, dmsnell, ndiego.
Fixes #59424.

This ticket was mentioned in Slack in #core-editor by fabiankaegy. View the logs.


6 months ago

Note: See TracTickets for help on using tickets.