Title: Understanding block attributes
Author: Michael Burridge
Published: September 14, 2023

---

 * [Showcase](https://wordpress.org/showcase/)
 * [Plugins](https://wordpress.org/plugins/)
 * [Themes](https://wordpress.org/themes/)
 * [Hosting](https://wordpress.org/hosting/)
 * [News](https://wordpress.org/news/)
    - [Learn WordPress](https://learn.wordpress.org/)
    - [Documentation](https://wordpress.org/documentation/)
    - [Education](https://wordpress.org/education/)
    - [Forums](https://wordpress.org/support/forums/)
    - [Developers](https://developer.wordpress.org/)
    - [Blocks](https://wordpress.org/blocks/)
    - [Patterns](https://wordpress.org/patterns/)
    - [Photos](https://wordpress.org/photos/)
    - [Openverse ︎](https://openverse.org/)
    - [WordPress.tv ︎](https://wordpress.tv/)
 *  - [About WordPress](https://wordpress.org/about/)
    - [Make WordPress](https://make.wordpress.org/)
    - [Events](https://events.wordpress.org/)
    - [Five for the Future](https://wordpress.org/five-for-the-future/)
    - [Enterprise](https://wordpress.org/enterprise/)
    - [Gutenberg ︎](https://wordpress.org/gutenberg/)
    - [Job Board ︎](https://jobs.wordpress.net/)
 * [Swag ︎](https://mercantile.wordpress.org/)
 * [Get WordPress](https://wordpress.org/download/)

Search in WordPress.org

 [ Get WordPress ](https://wordpress.org/download/)

[WordPress Developer Blog](https://developer.wordpress.org/news)

 * [Snippets](https://developer.wordpress.org/news/snippets/)
    - [Blocks](https://developer.wordpress.org/news/category/blocks/)
    - [Concepts](https://developer.wordpress.org/news/category/concepts/)
    - [Common APIs](https://developer.wordpress.org/news/category/common-apis/)
    - [Design](https://developer.wordpress.org/news/category/design/)
    - [Playground](https://developer.wordpress.org/news/category/playground/)
    - [Plugins](https://developer.wordpress.org/news/category/plugins/)
    - [Themes](https://developer.wordpress.org/news/category/themes/)
 *  - [About this site](https://developer.wordpress.org/news/about/)
    - [Updates](https://developer.wordpress.org/news/category/updates/)
    - [How to contribute](https://developer.wordpress.org/news/how-to-contribute/)
    - [Tips and guidelines](https://developer.wordpress.org/news/tips-and-guidelines-for-writers/)
 * [Subscribe](https://developer.wordpress.org/news/subscribe/)
 * [Developer Resources](https://developer.wordpress.org/)

 * [Snippets](https://developer.wordpress.org/news/snippets/)
    - [Blocks](https://developer.wordpress.org/news/category/blocks/)
    - [Concepts](https://developer.wordpress.org/news/category/concepts/)
    - [Common APIs](https://developer.wordpress.org/news/category/common-apis/)
    - [Design](https://developer.wordpress.org/news/category/design/)
    - [Playground](https://developer.wordpress.org/news/category/playground/)
    - [Plugins](https://developer.wordpress.org/news/category/plugins/)
    - [Themes](https://developer.wordpress.org/news/category/themes/)
 *  - [About this site](https://developer.wordpress.org/news/about/)
    - [Updates](https://developer.wordpress.org/news/category/updates/)
    - [How to contribute](https://developer.wordpress.org/news/how-to-contribute/)
    - [Tips and guidelines](https://developer.wordpress.org/news/tips-and-guidelines-for-writers/)
 * [Subscribe](https://developer.wordpress.org/news/subscribe/)
 * [Developer Resources](https://developer.wordpress.org/)

# 󠀁[Understanding block attributes](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/)󠁿

By [Michael Burridge](https://profiles.wordpress.org/mburridge/)

September 14, 2023

[Beginners](https://developer.wordpress.org/news/category/beginners/), [Blocks](https://developer.wordpress.org/news/category/blocks/),
[Concepts](https://developer.wordpress.org/news/category/concepts/)

There’s more to attributes than might initially meet the eye. A block can have as
many attributes as you like, and these are defined in the [attributes](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/)
property of the block’s `block.json` file. 

Block attributes are essentially variables that store data or content for the block.
Users can modify and update the values stored there. But where does the data or 
content come from?

An attribute can get its value from a defined `default`, or from user input. It 
can also be populated with a value retrieved from the stored content. The values
retrieved in this last way can, in fact, come from a variety of different places
in the stored content and can be retrieved in various ways. Let’s dig in.

**Table of contents**

 1. [The Anatomy of a simple block](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#the-anatomy-of-a-simple-block)
 2. [The source and selector properties](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#the-source-and-selector-properties)
 3. [Using HTML attributes as the source](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#using-html-attributes-as-the-source)
 4. [Using query as the source](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#using-query-as-the-source)
 5. [Conclusion](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#conclusion)

## The Anatomy of a simple block

Consider this simple block. It has a single [attribute](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#attributes),
namely `content`:

    ```javascript
    "attributes": {
      "content": {
        "type": "string"
      }
    }
    ```

The `Edit()` function implements a [`RichText`](https://developer.wordpress.org/block-editor/reference-guides/richtext/)
component that displays and updates the `content` attribute:

    ```javascript
    import { useBlockProps, RichText } from '@wordpress/block-editor';

    export default function Edit( { attributes, setAttributes } ) {
      return (
        <RichText
          { ...useBlockProps() }
          placeholder="Type something here..."
          tagName="div"
          value={ attributes.content }
          onChange={ ( val ) => setAttributes( { content: val } ) }
        />
      );
    }
    ```

The `save()` function does what you expect it to do and merely saves the content:

    ```javascript
    import { useBlockProps, RichText } from '@wordpress/block-editor';

    export default function save( { attributes } ) {
      return (
        <RichText.Content
          { ...useBlockProps.save() }
          tagName="div"
          value={ attributes.content }
        />
      );
    }
    ```

You’ll see this in your editor:

![Screenshot of simple block in editor with its toolbar. ](https://developer.wordpress.
org/news/files/2023/09/image.png)

Add some content and save your post and this is what you’ll see in the front end
of your site:

![view of simple block on the frontend.](https://developer.wordpress.org/news/files/
2023/09/image.png)

All well and good, and no surprises there. But let’s take a look at the code that
this block generates.

Click on the three dots in top right of the editor and select “Code editor” – or
use the handy [shortcut key combination](https://wordpress.org/documentation/article/keyboard-shortcuts-block-editor/#switch-between-visual-editor-and-code-editor)
to toggle between the visual editor and the code editor.

![Sidebar to change setting to code editor ](https://developer.wordpress.org/news/
files/2023/09/image-4.png)

This is what the code for this block looks like:

    ```wp-block-code
    <!-- wp:mb/attributes-test {"content":"My cool content!"} -->
    <div class="wp-block-mb-attributes-test">My cool content!</div>
    <!-- /wp:mb/attributes-test -->
    ```

Notice something here. The text you entered into the `RichText` component’s text
area appears twice – once in the attribute in the block delimiter, and then again
in the actual block content.

You may think that this is a bit redundant, which of course it is!

By default block attributes are stored in the block delimiter. But this needn’t 
be the case. They are only stored in the delimiter if no `source` property is provided
in the attribute definition.

## The `source` and `selector` properties

The `source` and `selector` properties can be used in tandem with each other to 
target part of the saved content of the block so that the attribute can be populated
with a value derived from the targeted area.

Let’s look at an example to see how these two properties work together. Add `source`
and `selector` properties to the definition of the `content` attribute:

    ```javascript
    "attributes": {
      "content": {
        "type": "string",
        "source": "text",
        "selector": "div"
      }
    }
    ```

Refresh the post in the editor and re-examine the code. Now you’ll see that the 
text only occurs once, in the block content, and it no longer appears in the block
delimiter:

    ```wp-block-code
    <!-- wp:mb/attributes-test -->
    <div class="wp-block-mb-attributes-test">My even cooler content!</div>
    <!-- /wp:mb/attributes-test -->
    ```

Yet, if you add a `console.log` statement to the `Edit()` function to view the attributes:

    ```javascript
    console.log( attributes );
    ```

You’ll see that the `content` attribute is still being populated:

![](https://developer.wordpress.org/news/files/2023/09/image-6.png)

The `content` attribute is in fact getting its value from the text, as defined by
the `source` property, within the `<div>` element, as defined by the `selector` 
property – so when you reload the post in the editor the `value` property of the`
RichText` component is populated with the text within the `<div>` element retrieved
from the saved content.

It’s important to ensure that you’re using the correct value for the `source` property
of your attribute. Let’s see why. 

Use the `RichText` component’s toolbar to make part of the text bold:

![bold text in simple block and block toolbar in editor](https://developer.wordpress.
org/news/files/2023/09/image-2.png)

If you inspect the code for this block now you’ll clearly see the `<strong>` tag
wrapping the emboldened text:

    ```wp-block-code
    <!-- wp:mb/attributes-test -->
    <div class="wp-block-mb-attributes-test">My <strong>even cooler</strong> content!</div>
    <!-- /wp:mb/attributes-test -->
    ```

Save the post, and check that it’s all looking good in the front-end.

![front end view of bold text](https://developer.wordpress.org/news/files/2023/09/
image-1.png)

Great! Now reload the post in the editor and…. oh yikes, that’s unexpected!

![screenshot of error message on mismatch between source and content in HTML](https://
developer.wordpress.org/news/files/2023/09/image-3.png)

Check in the browser console and you’ll see an error something like this:

    ```wp-block-code
    Content generated by `save` function:

    <div class="wp-block-mb-attributes-test">My even cooler content!</div>

    Content retrieved from post body:

    <div class="wp-block-mb-attributes-test">My <strong>even cooler</strong> content!</div>
    ```

This is because you now have markup embedded in the content, namely the `<strong
></strong>` tags and so it’s not pure text anymore. Change the value of the `source`
property in the attribute from `text` to `html`:

    ```javascript
    "attributes": {
      "content": {
        "type": "string",
        "source": "html",
        "selector": "div"
      }
    }
    ```

Now when you save the post and then reload it in the editor all will be well.

In effect the `text` value works akin to HTML’s [`textContent` property](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent),
and the `html` value works akin to HTML’s [`innerHTML` property](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML).

## Using HTML attributes as the source

As well as `text` and `html`, the `source` property can also take a value of an 
HTML attribute. In this case an additional `attribute` property must also be supplied.

Add a link to your text in the block:

![Screenshot of block with link in Editor](https://developer.wordpress.org/news/
files/2023/09/image-4.png)

If you now look at the block’s code you’ll see additional markup, namely the `<a
>` tag with the URL you chose in the `href` attribute: 

    ```wp-block-code
    <!-- wp:mb/attributes-test -->
    <div class="wp-block-mb-attributes-test">My <strong>even cooler</strong> content - powered by <a href="https://wordpress.org">WordPress</a>.</div>
    <!-- /wp:mb/attributes-test -->
    ```

Let’s get that URL into a block attribute. 

Add a new attribute to `block.json` and call it `embedded-link`:

    ```javascript
    "embedded-link": {
      "type": "string",
      "source": "attribute",
      "selector": "a",
      "attribute": "href"
    }
    ```

As mentioned earlier, when you specify `attribute` as the value of `source` then
you must supply an `attribute` property, in addition to the `selector` property.

So here we’re telling the block’s `embedded-link` attribute that its source is an
HTML attribute, and that it should select the `<a>` element and get its value from
the `href` attribute of that element.

_**Note:** in the foregoing paragraph it’s important to distinguish between what
is a [block attribute](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/)
and what is an [HTML attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes).
Don’t confuse the two!_

Make sure you’ve still got the `console.log( attributes )` statement in your  `Edit()`
function and reload the post containing your block in the editor. 

You should now see that the `attributes` object has two properties, `content` and`
embedded-link`:

 * `content` contains the markup entered by the user into the `RichText` component
   in the block
 * `embedded-link` contains the URL retrieved from the `href` attribute in the `
   <a>` element

    ```wp-block-code
    {content: 'My <strong>even cooler</strong> content - powered by <a href=\"https://wordpress.org\">WordPress</a>.', embedded-link: 'https://wordpress.org'}
    ```

It should be noted that the value of `selector` can be any HTML tag or any CSS selector
queryable with [querySelector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector).
So if in the markup of your block the `<a`> element had a class:

    ```language-markup
    <a class="wp-link" href=\"https://wordpress.org\">WordPress</a>
    ```

Then you could instead target it thus:

    ```javascript
    "embedded-link": {
      "type": "string",
      "source": "attribute",
      "selector": ".wp-link",
      "attribute": "href"
    }
    ```

Likewise, if the `<a>` element instead had a `name` attribute:

    ```language-markup
    <a name="wp-link" href=\"https://wordpress.org\">WordPress</a>
    ```

It could then be targeted like this: 

    ```javascript
    "embedded-link": {
      "type": "string",
      "source": "attribute",
      "selector": "a[name='wp-link']",
      "attribute": "href"
    }
    ```

This is all well and good for targeting single items of content, but what if you
want to get multiple items? For example, what if you want to get all the URLs from
all the `<a>` elements?

## Using `query` as the source

At the moment there’s only a single `<a>` element, so add an additional link to 
your block’s content:

![Screenshot of blog with link in editor](https://developer.wordpress.org/news/files/
2023/09/image-5.png)

Now your block’s code contains two links and looks something like this:

    ```wp-block-code
    <!-- wp:mb/attributes-test -->
    <div class="wp-block-mb-attributes-test">My <strong>even cooler</strong> content - powered by <a href="https://wordpress.org">WordPress</a>, made with <a href="https://en.wikipedia.org/wiki/Love">love</a>.</div>
    <!-- /wp:mb/attributes-test -->
    ```

Change the `embedded-link` attribute in `block.json` to instead be called `embedded-
links`, as this is more meaningful for storing multiple values. Define it as follows:

    ```javascript
    "embedded-links": {
      "type": "array",
      "source": "query",
      "selector": "a",
      "query": {
        "link": {
          "type": "string",
          "source": "attribute",
          "attribute": "href"
        }
      }
    }
    ```

Here we’ve changed the value of the `type` property to `array`, as we’re going to
be storing multiple values. We’ve also changed the `source` to `query`, while the`
selector` remains as `a`.

Just as when the `source` was set to `attribute` it was necessary to define an `
attribute` property, when the `source` is set as `query` you must define a `query`
property. 

The query yields an array of objects and it is in the definition of the `query` 
property that you define the structure of the object. Here, each object in the array
will have just one property, namely `link` which will store a string, and it will
use as its source the `href` attribute of each `<a>` element iterated over. 

Refresh the post in the editor and, assuming you still have the `console.log( attributes)`
statement in your  `Edit()` function, if you look in the browser console you should
see that both the URLs have been captured.

    ```wp-block-code
    embedded-links: Array(2)
      0: {link: 'https://wordpress.org'}
      1: {link: 'https://en.wikipedia.org/wiki/Love'}
    ```

Let’s also try getting the text content of the links. Add a `link-content` property
to the query:

    ```javascript
    "embedded-links": {
      "type": "array",
      "source": "query",
      "selector": "a",
      "query": {
        "link": {
          "type": "string",
          "source": "attribute",
          "attribute": "href"
        },
        "link-content": {
          "type": "string",
          "source": "text"
        }
      }
    }
    ```

And you should see something like this in the console:

    ```wp-block-code
    embedded-links: Array(2)
      0: {
        link: 'https://wordpress.org'.
        link-content: "WordPress"
      }
      1: {
        link: 'https://en.wikipedia.org/wiki/Love',
        link-content: "love"
      }
    ```

## Conclusion

Attributes provide a means for users to change the content and other data for the
block. If you can retrieve attribute values from within the block’s saved content
then you should do so by defining a `source` property to specify where you want 
to retrieve the value from. 

However, not all values can be retrieved from the content. It is still perfectly
legitimate to store attributes in the block’s delimiter. Values stored in the delimiter
would generally come from fields that you provide for the user, for example fields
within an `<InspectorControls>` component that appear in the settings sidebar. 

For the full low-down on working with attributes see the [Attributes page](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-attributes/)
in the Block Editor Handbook.

_Kudos for reviewing this post go to: [@bph](https://profiles.wordpress.org/bph/),
[@juanmaguitar](https://profiles.wordpress.org/juanmaguitar/), and [@thatdevgirl](https://profiles.wordpress.org/thatdevgirl/)_

**Categories:** [Beginners](https://developer.wordpress.org/news/category/beginners/),
[Blocks](https://developer.wordpress.org/news/category/blocks/), [Concepts](https://developer.wordpress.org/news/category/concepts/)

**Tags:** [Attributes](https://developer.wordpress.org/news/tag/attributes/), [Block development](https://developer.wordpress.org/news/tag/block-development/)

## 5 responses to “Understanding block attributes”

 1.  ![Deryck Avatar](https://secure.gravatar.com/avatar/58329bdc8447a2a9386b5c563e683c987284c73e74a7945c3d75ae874c6464de?
     s=40&d=identicon&r=g)
 2.  [Deryck](https://profiles.wordpress.org/deryck/)
 3.  [September 14, 2023](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#comment-1636)
 4.  Great article Michael. I always struggled with using HTML attributes as the source
     and this makes it so clear. Thanks!
 5.  [Reply](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/?replytocom=1636#respond)
 6.  ![Mateus Machado Luna Avatar](https://secure.gravatar.com/avatar/da4333e9e520f56887af71e68da6b6bdffb445f8c722e657ff3abe782500c0a7?
     s=40&d=identicon&r=g)
 7.  Mateus Machado Luna
 8.  [September 14, 2023](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#comment-1638)
 9.  I’ve been creating blocks since 5.0 and this is the first time that I actually
     understood how the query works! Thanks man!
 10. [Reply](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/?replytocom=1638#respond)
 11. ![Bridget Avatar](https://secure.gravatar.com/avatar/6e72783295d3e572b3d4870e0d1b653c644e96ccac1fab75b11775f15620afe6?
     s=40&d=identicon&r=g)
 12. Bridget
 13. [September 22, 2023](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#comment-1811)
 14. Thank you so much for writing this article. It cleared up how/when/why to use 
     attributes’ source and selector properties. I definitely had a few ah-ha moments
     reading through it.
 15. [Reply](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/?replytocom=1811#respond)
 16. ![Egor Avatar](https://secure.gravatar.com/avatar/afc3c677eb3222ef6c5e00c1310098fe3facc3c336d3f9a96d424e8ef9ce178a?
     s=40&d=identicon&r=g)
 17. Egor
 18. [September 30, 2024](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#comment-12326)
 19. don’t get how to work with type array.
      setAttributes() sets all values, render
     works, but on setAttribute() when POST request is triggered, new data for a attribute
     with type array is never sent, also tried query, effect is the same.
 20. [Reply](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/?replytocom=12326#respond)
 21. ![Matteo Avatar](https://secure.gravatar.com/avatar/f789bd2595548e4a51ddf03c0c3be4b0dee9712a1a7d15c5bac18821c580d435?
     s=40&d=identicon&r=g)
 22. Matteo
 23. [September 13, 2025](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/#comment-19383)
 24. It does not work for custom blocks that use HTML thead, tbody, or tfoot. Why?
 25. [Reply](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/?replytocom=19383#respond)

### Leave a Reply 󠀁[Cancel reply](https://developer.wordpress.org/news/2023/09/understanding-block-attributes/?output_format=md#respond)󠁿

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

 Save my name, email, and website in this browser for the next time I comment.

 Notify me of follow-up comments by email.

 Notify me of new posts by email.

Δ

## Have an idea for a new post?

### 󠀁[Learn how to contribute](https://developer.wordpress.org/news/how-to-contribute/)󠁿

Share your knowledge with fellow WordPress developers.

Search

### 󠀁[Review tips and guidelines](https://developer.wordpress.org/news/tips-and-guidelines-for-writers/)󠁿

Everything you need to know about writing for the Blog.

## Subscribe to the Blog

  Email Address

 Join 1,946 other subscribers

 * [About](https://wordpress.org/about/)
 * [News](https://wordpress.org/news/)
 * [Hosting](https://wordpress.org/hosting/)
 * [Privacy](https://wordpress.org/about/privacy/)

 * [Showcase](https://wordpress.org/showcase/)
 * [Themes](https://wordpress.org/themes/)
 * [Plugins](https://wordpress.org/plugins/)
 * [Patterns](https://wordpress.org/patterns/)

 * [Learn](https://learn.wordpress.org/)
 * [Documentation](https://wordpress.org/documentation/)
 * [Developers](https://developer.wordpress.org/)
 * [WordPress.tv ](https://wordpress.tv/)

 * [Get Involved](https://make.wordpress.org/)
 * [Events](https://events.wordpress.org/)
 * [Donate ](https://wordpressfoundation.org/donate/)
 * [Swag ](https://mercantile.wordpress.org/)

 * [WordPress.com ](https://wordpress.com/?ref=wporg-footer)
 * [Matt ](https://ma.tt/)
 * [bbPress ](https://bbpress.org/)
 * [BuddyPress ](https://buddypress.org/)

 The WordPress® trademark is the intellectual property of the WordPress Foundation.

 * [Visit our X (formerly Twitter) account](https://www.x.com/WordPress)
 * [Visit our Bluesky account](https://bsky.app/profile/wordpress.org)
 * [Visit our Mastodon account](https://mastodon.world/@WordPress)
 * [Visit our Threads account](https://www.threads.net/@wordpress)
 * [Visit our Facebook page](https://www.facebook.com/WordPress/)
 * [Visit our Instagram account](https://www.instagram.com/wordpress/)
 * [Visit our LinkedIn account](https://www.linkedin.com/company/wordpress)
 * [Visit our TikTok account](https://www.tiktok.com/@wordpress)
 * [Visit our YouTube channel](https://www.youtube.com/wordpress)
 * [Visit our Tumblr account](https://wordpress.tumblr.com/)