Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Block support: Add anchor support for dynamic blocks
  • Loading branch information
t-hamano committed Dec 23, 2025
commit ade13f4bc600c74ddcbd3941912ec853a47afe6c
2 changes: 1 addition & 1 deletion docs/reference-guides/block-api/block-supports.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function Edit( { attributes } ) {
- Type: `boolean`
- Default value: `false`

Anchors let you link directly to a specific block on a page. This property adds a field to define an id for the block and a button to copy the direct link. _Important: It doesn't work with dynamic blocks yet._
Anchors let you link directly to a specific block on a page. This property adds a field to define an id for the block and a button to copy the direct link.

```js
// Declare support for anchor links.
Expand Down
136 changes: 68 additions & 68 deletions docs/reference-guides/core-blocks.md

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions lib/block-supports/anchor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Anchor block support flag.
*
* @package gutenberg
*/

/**
* Registers the anchor block attribute for block types that support it.
*
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_anchor_support( $block_type ) {
$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );

if ( ! $has_anchor_support ) {
return;
}

if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}

if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) {
$block_type->attributes['anchor'] = array(
'type' => 'string',
);
}
}

/**
* Add the anchor id to the output.
*
* @param WP_Block_Type $block_type Block Type.
* @param array $block_attributes Block attributes.
*
* @return array Block anchor id.
*/
function gutenberg_apply_anchor_support( $block_type, $block_attributes ) {
if ( ! $block_attributes ) {
return array();
}

$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );
if ( ! $has_anchor_support ) {
return array();
}

$has_anchor = array_key_exists( 'anchor', $block_attributes );
if ( ! $has_anchor || empty( $block_attributes['anchor'] ) ) {
return array();
}

return array( 'id' => $block_attributes['anchor'] );
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'anchor',
array(
'register_attribute' => 'gutenberg_register_anchor_support',
'apply' => 'gutenberg_apply_anchor_support',
)
);
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/block-supports/background.php';
require __DIR__ . '/block-supports/block-style-variations.php';
require __DIR__ . '/block-supports/aria-label.php';
require __DIR__ . '/block-supports/anchor.php';
require __DIR__ . '/block-supports/block-visibility.php';

// Client-side media processing.
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/archives/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
}
},
"supports": {
"anchor": true,
"align": true,
"__experimentalBorder": {
"radius": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/avatar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"usesContext": [ "postType", "postId", "commentId" ],
"supports": {
"anchor": true,
"html": false,
"align": true,
"alignWide": false,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/breadcrumbs/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"usesContext": [ "postId", "postType", "templateSlug" ],
"supports": {
"anchor": true,
"html": false,
"align": [ "wide", "full" ],
"spacing": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/calendar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
}
},
"supports": {
"anchor": true,
"align": true,
"html": false,
"color": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"usesContext": [ "enhancedPagination" ],
"supports": {
"anchor": true,
"align": true,
"html": false,
"spacing": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"usesContext": [ "commentId" ],
"supports": {
"anchor": true,
"html": false,
"spacing": {
"margin": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-content/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}
},
"supports": {
"anchor": true,
"color": {
"gradients": true,
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"usesContext": [ "commentId" ],
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-edit-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}
},
"supports": {
"anchor": true,
"html": false,
"color": {
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-reply-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}
},
"supports": {
"anchor": true,
"color": {
"gradients": true,
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-template/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"textdomain": "default",
"usesContext": [ "postId" ],
"supports": {
"anchor": true,
"align": true,
"html": false,
"reusable": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"usesContext": [ "postId", "comments/paginationArrow" ],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"color": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"textdomain": "default",
"usesContext": [ "postId" ],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"color": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"usesContext": [ "postId", "comments/paginationArrow" ],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"color": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"comments/paginationArrow": "paginationArrow"
},
"supports": {
"anchor": true,
"align": true,
"reusable": false,
"html": false,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/comments-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
},
"supports": {
"anchor": false,
"anchor": true,
"align": true,
"html": false,
"__experimentalBorder": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
}
},
"supports": {
"anchor": true,
"align": [ "wide", "full" ],
"html": false,
"color": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/embed/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
}
},
"supports": {
"anchor": true,
"align": true,
"spacing": {
"margin": true
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/footnotes/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"textdomain": "default",
"usesContext": [ "postId", "postType" ],
"supports": {
"anchor": true,
"__experimentalBorder": {
"radius": true,
"color": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/home-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"style"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"typography": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/latest-comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
}
},
"supports": {
"anchor": true,
"align": true,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/latest-posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
}
},
"supports": {
"anchor": true,
"align": true,
"html": false,
"color": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/loginout/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"className": true,
"color": {
"background": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/math/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"keywords": [ "equation", "formula", "latex", "mathematics" ],
"textdomain": "default",
"supports": {
"anchor": true,
"html": false,
"__experimentalBorder": {
"color": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/more/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
}
},
"supports": {
"anchor": true,
"customClassName": false,
"className": false,
"html": false,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"style"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"__experimentalSlashInserter": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation-submenu/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"style"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"typography": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"maxNestingLevel": "maxNestingLevel"
},
"supports": {
"anchor": true,
"align": [ "wide", "full" ],
"ariaLabel": true,
"contentRole": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/nextpage/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"parent": [ "core/post-content" ],
"textdomain": "default",
"supports": {
"anchor": true,
"customClassName": false,
"className": false,
"html": false,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/page-list-item/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"openSubmenusOnClick"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"lock": false,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/page-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"openSubmenusOnClick"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"typography": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"spacing": {
"margin": true,
"padding": true
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-author-name/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"html": false,
"spacing": {
"margin": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-author/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"usesContext": [ "postType", "postId", "queryId" ],
"supports": {
"anchor": true,
"html": false,
"spacing": {
"margin": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-comments-form/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"usesContext": [ "postId", "postType" ],
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-comments-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"html": false,
"color": {
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-content/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"align": [ "wide", "full" ],
"html": false,
"layout": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
Loading
Loading