Skip to content

Fix: Use add_filter() for get_block_type_variations hook#76297

Merged
talldan merged 1 commit intoWordPress:trunkfrom
apermo:fix/navigation-link-add-filter-hook
Mar 9, 2026
Merged

Fix: Use add_filter() for get_block_type_variations hook#76297
talldan merged 1 commit intoWordPress:trunkfrom
apermo:fix/navigation-link-add-filter-hook

Conversation

@apermo
Copy link
Copy Markdown
Contributor

@apermo apermo commented Mar 8, 2026

What?

Closes #76296

Changes add_action() to add_filter() for the get_block_type_variations hook in the Navigation Link block.

Why?

The get_block_type_variations hook is fired via apply_filters() in WordPress Core, making it a filter hook. Using add_action() is semantically incorrect, even though it works because add_action() calls add_filter() internally.

The hook is invoked here:
https://github.com/WordPress/wordpress-develop/blob/64086b2a15ef4ccf6443de2056646da1ee17d56d/src/wp-includes/class-wp-block-type.php#L608-L616

The callback function already returns as expected:

/**
* Filters the registered variations for a block type.
* Returns the dynamically built variations for all post-types and taxonomies.
*
* @since 6.5.0
*
* @param array $variations Array of registered variations for a block type.
* @param WP_Block_Type $block_type The full block type object.
* @return array Numerically indexed array of block variations.
*/
function block_core_navigation_link_filter_variations( $variations, $block_type ) {
if ( 'core/navigation-link' !== $block_type->name ) {
return $variations;
}
$generated_variations = block_core_navigation_link_build_variations();
/*
* IMPORTANT: Order matters for deduplication.
*
* The variations returned from this filter are bootstrapped to JavaScript and
* processed by the block variations reducer. The reducer uses `getUniqueItemsByName()`
* (packages/blocks/src/store/reducer.js:51-57) which keeps the FIRST variation with
* a given 'name' and discards later duplicates when processing the array in order.
*
* By placing generated variations first in `array_merge()`, the improved
* labels (e.g., "Product link" instead of generic "Post Link") are processed first
* and preserved. The generic incoming variations are then discarded as duplicates.
*
* Why `array_merge()` instead of manual deduplication?
* - Both arrays use numeric indices (0, 1, 2...), so `array_merge()` concatenates
* and re-indexes them sequentially, preserving order
* - The reducer handles deduplication, so it is not needed here
* - This keeps the PHP code simple and relies on the established JavaScript behavior
*
* See: https://github.com/WordPress/gutenberg/pull/72517
*/
return array_merge( $generated_variations, $variations );
}

How?

Replaced add_action() with add_filter() in packages/block-library/src/navigation-link/index.php.

Testing Instructions

  1. No functional change — add_action() is an alias for add_filter() internally.
  2. Verify the Navigation Link block still works correctly with its variations (e.g. Page Link, Post Link, Category Link, Tag Link).

Testing Instructions for Keyboard

N/A — no UI changes.


This PR was authored by Claude Code (AI), steered and reviewed by @apermo.

The `get_block_type_variations` hook is invoked via
`apply_filters()`, so it should be registered with
`add_filter()` instead of `add_action()`.
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 8, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: apermo <apermo@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions bot added [Package] Block library /packages/block-library First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository labels Mar 8, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 8, 2026

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @apermo! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

Copy link
Copy Markdown
Contributor

@talldan talldan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for fixing.

@talldan talldan added [Type] Bug An existing feature does not function as intended [Block] Navigation Link Affects the Navigation Link Block props-bot Manually triggers Props Bot to ensure the list of props is up to date. labels Mar 9, 2026
@github-actions github-actions bot removed the props-bot Manually triggers Props Bot to ensure the list of props is up to date. label Mar 9, 2026
@talldan talldan merged commit ea1cd28 into WordPress:trunk Mar 9, 2026
53 of 55 checks passed
@github-actions github-actions bot added this to the Gutenberg 22.8 milestone Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Navigation Link Affects the Navigation Link Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block library /packages/block-library [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: add_action() used instead of add_filter() for get_block_type_variations

2 participants