Skip to content
pinegrow.com
Docs menuWordPress

Adding attributes and controls

Attributes are editable properties of a block. Editable attributes make blocks much more useful, because we can use the same block type with different content.

On this page

Our About me block will have editable title, description, image and the button (we will take care of the counters in the next part).

Title

To make the heading editable, select the <h3> element and add Block Attributes action: click + next to WordPress Actions and choose Blocks Smart Actions › Block Attributes. 

The Nothing weird. Ever. heading of the Ingredients block selected, with the Add action list open on Blocks Smart Actions and Block Attributes highlighted, with its tooltip, and a callout on the + next to WORDPRESS ACTIONS that opens the list.

Each attribute needs an id, such as title. Attribute id needs to be unique within its block.

The Ingredients block heading with Block Attributes - Smart: Block attribute 1 with Attribute id title, Title Title and Use as Content.

Next, we set how the attribute is used on the selected element. Will it set the element inner content, add a class, set a style, element attribute…?

The Use as list of the title block attribute open: Content checked, Image, Background image, Link, Style, Attribute, Class, Tag and None.

For the title attribute we will select Content. The attribute value will replace the inner content of the <h3> element.

Title is a human readable name of the attribute.

What to do if the attribute value is not set?

Click Attribute options to further customize the attribute.

Attribute options of the title block attribute, opened with the ATTRIBUTE OPTIONS toggle (marked by a box), with the If empty list open: Use default value checked and Remove the element, above Use post property, Default value, Control type and Expression.

We have two options: use the default value or remove the element from the output.

Default values are taken from the source HTML element. For example, if the attribute is used as the element content, its default value will be the element content found in the source HTML. In this case we will leave the setting as it is and use the default value.

Description

To make the description editable, select the <p> element and add Block Attributes action with the id description. Here we will open Attribute options and set Control type to Rich Text, so that we can format the text if needed.

The description block attribute of the paragraph with Attribute options open and the Control type list showing Auto, Input, Textarea, Rich Text (checked), Select, Toggle, Link, Image, Color and None (use with expressions).

Image

To make the image editable, we select the <img> element and add the Block Attributes action. The attribute will be used as the value of src attribute and image selector control will be displayed in the sidebar.

The can image in the Fizz Hero block selected, with Block Attributes - Smart: Block attribute 1 with Attribute id image, Title Image, Use as Image, Image size large and Use image ALT Auto.

Button

Then select the button <a> and add Block Attributes action. Select Use as “Link” to make the href editable:

The Shop now button of the Fizz Hero block selected, with Block attribute 1 set to Attribute id button, Title Button and Use as Link, so the link is editable in the block editor.

Let’s also make the label editable. Click on the Add attribute button on the Block Attributes action to add another editable attribute to the selected element:

Block attribute 1 of the button with Attribute id button and Use as Link, and the Add attribute button below it for adding another editable attribute to the same element.

Here, we will make the Content editable:

The Shop now button with two block attributes: button used as Link makes the link editable, and button_label used as Content makes the button text editable.

The block is now editable!

Export the project and reload the Block editor.

Now we can edit the title and description directly on the block, and customize all its properties in the sidebar.

WordPress block editor on the Front Page showing the surfing heading and text, with a callout to edit texts on the page view.

Learn more about Block Attributes action.

Next step

We need to make the counters editable as well. We could use a fixed number of counters and make their numbers and descriptions editable with attributes such as Counter number 1, Counter label 1, Counter number 2…

But, we have a much better solution: nested blocks.

Next step: Adding inner blocks (blocks within the block).