Skip to content

Block Supports: Don't merge unique attributes (ID, aria-label) in get_block_wrapper_attributes #75251

Closed

Description

@hadamlenz

Description

This feature is not backward compatible and creates a space in the anchor, which is not valid html.
if an anchor is set in the block editor, say "my-anchor".
if supports.anchor is set and an attribute "anchor" of type string is set in attributes in block.json
the resulting $block_wrapper_attributtes has an id of "my-anchor my-anchor"
Going back to 22.2.0 fixes the issue, but this means we cannot run the plugin for our own dynamic blocks. If there is a better way, please advise

Step-by-step reproduction instructions

  1. in block.json set supports.anchor=true
  2. in block.json set a string type attribute to hold the anchor
  3. in render.php set the id of get_block_wrapper_attributes()

Screenshots, screen recording, code snippet

Image
$block_wrapper_attributtes = get_block_wrapper_attributes(['id'=>$attributes['anchor']]);

results in

class="..." id="my-anchor my-anchor"

Environment info

WordPress 6.9.1, Gutenberg 22.5.0, 2025 theme
Most current Chrome
16-inch, 2021 M1 MBP running Tahoe 26.2

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Activity

  1. added
    [Type] BugAn existing feature does not function as intended
    on Feb 5, 2026
  2. t-hamano commented on Feb 5, 2026

    @t-hamano
    Contributor

    @hadamlenz Thanks for the report.

    $block_wrapper_attributtes = get_block_wrapper_attributes(['id'=>$attributes['anchor']]);

    results in

    class="..." id="my-anchor my-anchor"
    

    get_block_wrapper_attributes should automatically generate the id attribute based on the anchor data, so I believe you shouldn't need to add any arguments.

    Could you try the code below?

    $block_wrapper_attributtes = get_block_wrapper_attributes();
  3. hadamlenz commented on Feb 5, 2026

    @hadamlenz
    Author

    I don't think ID should be included in the attributes that get concatenated with a space. if a dev sets the ID in get_block_wrapper_attributes() that should not create additional spaces. that is not valid html

    $attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ] . ' ' . $new_attributes[ $attribute_name ];
  4. hadamlenz commented on Feb 5, 2026

    @hadamlenz
    Author

    if the ID gets set automatically, and we are forced to use it, and we cannot manipulate it, and we cannot test for it later, you've taken away methods for developers to set the ID. this is important in places where no ID is set by an author, but some mechanism needs an ID. Again, this change is not backward compatible, so if other devs set the ID in get_block_wrapper_attributes, there will be a lot of blocks broken. This worked prior to 22.3 just fine.

  5. Mamaduka commented on Feb 5, 2026

    @Mamaduka
    Member

    I think this is a valid point. The ID (and maybe aria-label) is different from the other attributes in attributes_to_merge, as it must be unique and users might want to override it programmatically.

    To maintain backward compatibility, we could update get_block_wrapper_attributes so that if it receives id, it is used instead of the native anchor.

    Though I think similar blocks should also disablesupports.anchor, otherwise it will create a confusing UX.

  6. hadamlenz commented on Feb 5, 2026

    @hadamlenz
    Author

    something like this is needed

    if(isset($extra_attributes["id"])){
     $attributes["id"] = $extra_attributes["id"];
    } else {
     $attributes["id"] = $new_attributes["id"];
    }
  7. t-hamano commented on Feb 5, 2026

    @t-hamano
    Contributor

    Thanks for the detailed explanation. Perhaps the following changes are needed in the core:

    diff --git a/src/wp-includes/class-wp-block-supports.php b/src/wp-includes/class-wp-block-supports.php
    index ec5bc9c8d6..3215845498 100644
    --- a/src/wp-includes/class-wp-block-supports.php
    +++ b/src/wp-includes/class-wp-block-supports.php
    @@ -188,6 +188,21 @@ function get_block_wrapper_attributes( $extra_attributes = array() ) {
                            continue;
                    }
     
    +               /*
    +                * The "id" and "aria-label" attributes are not space-separated
    +                * lists like "class". When both block supports and the caller
    +                * provide a value, always prefer the value from $extra_attributes
    +                * to avoid duplicated values or unexpected concatenation.
    +                */
    +               if ( 'id' === $attribute_name || 'aria-label' === $attribute_name ) {
    +                       if ( ! empty( $extra_attributes[ $attribute_name ] ) ) {
    +                               $attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ];
    +                       } else {
    +                               $attributes[ $attribute_name ] = $new_attributes[ $attribute_name ];
    +                       }
    +                       continue;
    +               }
    +
                    if ( empty( $new_attributes[ $attribute_name ] ) ) {
                            $attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ];
                            continue;
  8. t-hamano commented on Feb 5, 2026

    @t-hamano
    Contributor

    Let's aim to fix this issue in the 7.0 release.

  9. hadamlenz commented on Feb 5, 2026

    @hadamlenz
    Author

    thanks yall!

  10. changed the title [-]in adding anchor support for dynamic blocks, anchors are now doubled when ID is set in get_block_wrapper_attributes[/-] [+]Block Supports: Don't merge unique attributes (ID, aria-label) in get_block_wrapper_attributes[/+] on Feb 5, 2026
  11. t-hamano commented on Feb 5, 2026

    @t-hamano
    Contributor

    @hadamlenz, your wordpress.org username is adrock42, correct? This is to ensure that all props are listed.

  12. t-hamano commented on Feb 5, 2026

    @t-hamano
    Contributor

    Core ticket for tracking this issue: https://core.trac.wordpress.org/ticket/64603

  13. removed
    [Type] BugAn existing feature does not function as intended
    on Feb 5, 2026
  14. hadamlenz commented on Feb 5, 2026

    @hadamlenz
    Author

    @hadamlenz, your wordpress.org username is adrock42, correct? This is to ensure that all props are listed.

    that is correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Block APIAPI that allows to express the block paradigm.

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions