Why do you need attributes in Gutenberg block?

Why do you need attributes in Gutenberg block?

Anything you need saved for your block (input from the user/editor) requires an attribute. It’s up you how you structure your data, by defining separate attributes for each or bunch them all up in an object. It will just be a difference in how you fetch their data and how you update them.

How to create a Gutenberg block in WordPress?

WordPress’ RichText component gives you a “borderless” textarea with text formatting support. You might prefer using this instead of a (ugly?) standard text input or textarea.

When to use a prop in Gutenberg block?

When you work with RichText you most likely want to control the HTML wrapper around the text, for example a or a , both in frontend and in editor. For that we can use a prop called tagName. The component RichText allows several other props as well.


How to save an attribute in WordPress comment block?

To solve this we need to work with the attribute property source. The source property allowing WordPress to extract and interpret the content directly from the post content. If an attribute has no source set, it will be saved in and extracted from the HTML comment block.

How to add custom settings to WordPress Gutenberg blocks?

In come cases of settings that affect a block’s design we need to add inline style on the block’s wrapping node. Just class name won’t do. For example if you add a custom color setting and the user selects a custom color (from colorpicker), you can’t solve this by adding a class – you need inline style.

Can You filter wrapping class name in Gutenberg?

As mentioned above we can filter the block’s wrapping class name as this is a prop that is passed to all Gutenberg blocks. But unfortunately there are some blocks that don’t apply the block’s class at all in edit. One example is the Cover block.

How to filter save props in WordPress Gutenberg?

Filter the block’s save props. We can adjust the props for save, which is responsible for both saving the block and rendering it in frontend (outside of editor). In our case we want to add a class or inline style.

How does the edit and save function work in Gutenberg?

The magic really happens within the edit and save functions within the block configuration object. The attributes passed to the block configuration object will define the block and the data it will be using. A lot of the properties passed to the block configuration object help the end user understand and find your block. Some examples:

Are there any plugins for the Gutenberg editor?

The Gutenberg Editor already includes built-in blocks for all types of content elements. These blocks make it easier to add common elements like buttons, cover images, tables, and more. In the older versions of WordPress, you had to install plugins to add these common web elements.