Attributes for Blocks

Mô tả

This plugin adds additional advanced inspector controls to Gutenberg blocks that allow to add any custom HTML attributes to the block’s front-end output. This allows you to add inline styles to fine-tune the block’s appearance, set aria attributes to improve your site’s accessibility, add data attributes to integrate with any JavaScript modules or even JavaScript DOM event attributes such as onclick, onchange or onload.


Ảnh màn hình

  • Adding style attribute to paragraph block

Cài đặt

Install via admin dashboard

  1. Go to your WordPress admin dashboard -> Plugins.
  2. Click “Add New”.
  3. Click “Upload Plugin”.
  4. Select the file.
  5. Click “Install Now”.
  6. Activate the plugin from WordPress admin dashboard -> Plugins.

Manual install via FTP upload

  1. Upload the folder “attributes-for-blocks” from file to your WordPress installations ../wp-content/plugins folder.
  2. Activate the plugin from WordPress admin dashboard -> Plugins.

Hỏi đáp

How do I add an attribute?

In your selected block’s inspector controls (Block settings) scroll all the way to the bottom and click on “Advanced”. It should contain a section called “Additional attributes”.
Type an attribute name into the “Add attribute” field and press “Add” to add an attribute for the block. A new input with the attribute’s name should appear below, into which you can optionally insert the attribute value.
Example attributes: style, title, target, class, id, onClick, data-*, aria-*.

How does it work?

For regular blocks, attributes are added to the block save content’s root element, meaning they will be rendered only on the front end and not in the editor. For dynamic blocks the attributes are added via render_callback function and they may also be applied in the editor, depending if the block is rendered server or client side.

Does it work for every block?

It should work with normal blocks that render a valid WP Element that can utilize the blocks.getSaveContent.extraProps filter as well as dynamic blocks that utilize a render_callback. Third party blocks that do something unorthodox may not work.
Known unsupported blocks

Usage with Alpine.js

@ prefix in an attribute name is used for “override” mode in this plugin, for Alpine.js attributes use x-on:click instead of @click or use the shorthand syntax with two @ characters instead of one: @@click.

Disable block support

The afb_unsupported_blocks filter can be used in your child theme’s functions.php file to disable block support for adding additional attributes.

add_filter('afb_unsupported_blocks', function($blocks) {
    $blocks[] = 'core/button';
    return $blocks;

What happens when I disable this plugin?

Blocks with custom attributes may become invalid, depending on which attributes you’ve added. From there you can recover the block without the custom attributes by clicking “Attempt Block Recovery” or keep the block with custom attributes as HTML by choosing “Convert to HTML”. If you don’t want to risk blocks becoming invalid you need to remove all custom attributes before disabling the plugin.

Đánh giá

14 Tháng Hai, 2024
Thank you very much for providing this plugin, I have been able to use SAL scroll animation library with it works perfectly! Hope you keep going with this Plugin for future Wordpress versions. Thank YOU!
4 Tháng Một, 2024
I absolutely love this plugin! Does what it says easily and reliably, doesn't add any bloat. Amazing.
21 Tháng Mười Một, 2023
This adds considerable versatility to the WordPress blocks editor — I wanted to use Bootstrap in my current custom theme, but didn't want to integrate Bootstrap fully, only using it within blocks when needed. Things like the dark mode (data-bs-theme="dark") can be added in, you can use onClick to utilize JavaScript (I used that to click open Bootstrap cards, etc), and really much more. Saved me a lot of time having to do things in a much more manual way. Highly recommend this plugin, works great.
Đọc tất cả 17 đánh giá

Người đóng góp & Lập trình viên

“Attributes for Blocks” là mã nguồn mở. Những người sau đã đóng góp vào plugin này.

Những người đóng góp

“Attributes for Blocks” đã được dịch qua 3 ngôn ngữ. Cảm ơn những người tham gia dịch vì đóng góp của họ.

Dịch “Attributes for Blocks” sang ngôn ngữ của bạn.

Muốn tham gia phát triển?

Duyệt code, check out SVN repository, hoặc theo dõi nhật ký phát triển qua RSS.

Nhật ký thay đổi


  • Use WP_HTML_Tag_Processor for adding HTML attributes.
  • Remove afb_sanitize_attribute_key and afb_sanitize_attribute_value filters (now handled by WP_HTML_Tag_Processor).
  • Use render_block filter to apply attributes instead of overriding block’s render_callback.
  • Move all PHP code to main file for simplicity.
  • Add $attribute param to afb_attribute_separator filter.
  • Remove uppercase text transform from attribute input labels, use monospace font for value.
  • Add button to edit attributes in a modal for more space.
  • Update @wordpress/* packages.
  • Regression: for blocks that render multiple root elements attributes are only applied to the first one.


  • Add afb_sanitize_attribute_key and afb_sanitize_attribute_value filters.
  • Catch errors when using invalid characters in attribute name/value.
  • Update @wordpress/* packages.


  • Update @wordpress/* packages.
  • Test with WordPress 6.0.
  • Convert advanced style attribute editor to TypeScript and refactor.
  • Fix duplicate attribute values being output when the block has both JS and PHP render functions.
  • Add GitHub link.
  • Remove src folder from plugin.


  • Add advanced editor for style attribute.
  • Remove jQuery.


  • Fix special character encoding for dynamic blocks.