{"id":72,"date":"2024-10-03T11:59:59","date_gmt":"2024-10-03T15:59:59","guid":{"rendered":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/?page_id=72"},"modified":"2025-08-15T14:06:50","modified_gmt":"2025-08-15T18:06:50","slug":"attributes","status":"publish","type":"page","link":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/handbook\/wp-core\/block-architecture\/attributes\/","title":{"rendered":"Block Attributes"},"content":{"rendered":"\n<p>Block attributes provide information about the data stored by a block. For example, rich content, a list of image URLs, a background color, or a button title.<\/p>\n\n\n\n<p>A block can contain any number of attributes, and these are specified by the&nbsp;<code>attributes<\/code>&nbsp;field \u2013 an object where each key is the name of the attribute, and the value is the attribute definition.<\/p>\n\n\n\n<p>The attribute definition will contain, at a minimum, either a&nbsp;<code>type<\/code>&nbsp;or an&nbsp;<code>enum<\/code>. There may be additional fields.<\/p>\n\n\n\n<h2>Type or Enum<\/h2>\n\n\n\n<p>An Attribute must have either a <code>type<\/code> or <code>enum<\/code> as outlined in the documentation here: <a href=\"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-attributes\/#type-validation\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-attributes\/#type-validation<\/a><\/p>\n\n\n\n<p>A <code>type<\/code> would indicate the type of data stored in the attribute such as <code>string<\/code>, <code>integer<\/code>, <code>boolean<\/code>, <code>array<\/code>, and <code>object<\/code>.<\/p>\n\n\n\n<p>An <code>enum<\/code> contains an array of allowed values such as: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91; 'large', 'small', 'tiny' ]<\/code><\/pre>\n\n\n\n<h2>Attribute Source<\/h2>\n\n\n\n<p>Attributes can store and source their data from a number of different places. If no source is set, the data is stored in the block&#8217;s HTML comment delimiter. <\/p>\n\n\n\n<p>Most blocks we build will be Dynamic and use a PHP template to render on the frontend. For Dynamic blocks you must store the attribute in the HTML comment so don&#8217;t set a source. <\/p>\n\n\n\n<p>For static blocks the source of the attribute can be the markup of the block. However this causes serious issues later if you need to convert that block from static to dynamic so it&#8217;s best to store your attribute in the HTML comment of the block. Don&#8217;t set an attribute source unless you have a very good and particular reason. <\/p>\n\n\n\n<p>To store attributes in the markup, set the <code>source<\/code> and a <code>selector<\/code> value so that the parser can find the data in the markup. These selectors are essentially CSS selectors so you can use something as simple as <code>img<\/code> or more complex such as <code>img.someclass<\/code>, or <code>.wp-block-some-example h2<\/code>.<\/p>\n\n\n\n<h2>Attributes stored in Post Meta<\/h2>\n\n\n\n<p>Prior to WordPress 5.4 Attribute sources could be stored in postmeta. This was deprecated in favor of a new method using <code>EntityProvider<\/code> and hooks. The existing blocks that use the old method should still work for some time, but we should convert them to the new method. See: <a href=\"https:\/\/make.wordpress.org\/core\/2020\/03\/02\/general-block-editor-api-updates\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/make.wordpress.org\/core\/2020\/03\/02\/general-block-editor-api-updates\/<\/a><\/p>\n\n\n\n<p><em>Note: In WordPress 6.5 or so a new Block Bindings API is being added which appears to be taking this further and making more changes to how block attributes and custom fields \/ post meta are connected together. <\/em><\/p>\n\n\n\n<h2>Attributes vs State<\/h2>\n\n\n\n<p>It is important to note that attributes should be used for data that needs to be saved with the block. There are times when there is a need for saving the state of something in the block when it is being edited. For instance if a UI element is open in a component or if some piece of the block should be shown\/hidden in the editor. These types of state changes should generally not be stored as an attribute but instead can be managed and &#8220;saved&#8221; in the editor using the <code>useState()<\/code> hook. <\/p>\n\n\n\n<p class=\"has-pale-pink-background-color has-background\">@todo embed an example<\/p>\n\n\n\n<p>When a block is parsed this definition will be used to extract data from the block content. Anything that matches will be available to your block through the&nbsp;<code>attributes<\/code>&nbsp;prop. Example:<\/p>\n\n\n\n<p class=\"has-pale-pink-background-color has-background\"><meta charset=\"utf-8\">@todo embed an example<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>See <a href=\"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/block-api\/block-attributes\/\">WordPress Developer Resources: Attributes<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Block attributes provide information about the data stored by a block. For example, rich content, a list of image URLs, a background color, or a button title. A block can contain any number of attributes, and these are specified by the&nbsp;attributes&nbsp;field \u2013 an object where each key is the name of the attribute, and the [&hellip;]<\/p>\n","protected":false},"author":3670,"featured_media":0,"parent":98,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"status-is-a-reserved-term":[5],"assignee":[],"_links":{"self":[{"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages\/72"}],"collection":[{"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/users\/3670"}],"replies":[{"embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":10,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages\/72\/revisions"}],"predecessor-version":[{"id":732,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages\/72\/revisions\/732"}],"up":[{"embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/pages\/98"}],"wp:attachment":[{"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"status-is-a-reserved-term","embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/status-is-a-reserved-term?post=72"},{"taxonomy":"assignee","embeddable":true,"href":"https:\/\/id-developer-upgrade-58.cms-devl.bu.edu\/gutenberg\/wp-json\/wp\/v2\/assignee?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}