Bird
Raised Fist0
Wordpressframework~5 mins

Block attributes and controls in Wordpress

Choose your learning style10 modes available
Introduction

Block attributes store the settings or data of a block. Controls let users change these settings easily in the editor.

When you want users to customize text, colors, or images inside a block.
When you need to save user choices like font size or alignment.
When you want to add buttons or sliders to change block content.
When you want to keep block data consistent and editable.
When building reusable blocks that need flexible options.
Syntax
Wordpress
registerBlockType('namespace/block-name', {
  attributes: {
    attributeName: {
      type: 'string',
      default: 'default value'
    }
  },
  edit: (props) => {
    const { attributes, setAttributes } = props;
    return (
      <TextControl
        label="Label"
        value={attributes.attributeName}
        onChange={(value) => setAttributes({ attributeName: value })}
      />
    );
  },
  save: (props) => {
    return <p>{props.attributes.attributeName}</p>;
  },
});

Attributes define what data the block holds and how it is saved.

Controls like TextControl let users change attributes in the editor.

Examples
This defines a text attribute named content with a default value.
Wordpress
attributes: {
  content: { type: 'string', default: 'Hello!' }
}
This control lets the user edit the content attribute with a text input.
Wordpress
<TextControl
  label="Content"
  value={attributes.content}
  onChange={(value) => setAttributes({ content: value })}
/>
A boolean attribute to store true/false state.
Wordpress
attributes: {
  isActive: { type: 'boolean', default: false }
}
A toggle switch control to change the boolean attribute.
Wordpress
<ToggleControl
  label="Active"
  checked={attributes.isActive}
  onChange={(value) => setAttributes({ isActive: value })}
/>
Sample Program

This block lets users type a message and choose to show or hide it. The message and toggle are saved as attributes.

Wordpress
import { registerBlockType } from '@wordpress/blocks';
import { TextControl, ToggleControl } from '@wordpress/components';

registerBlockType('myplugin/simple-block', {
  title: 'Simple Block',
  icon: 'smiley',
  category: 'common',
  attributes: {
    message: { type: 'string', default: 'Hello, world!' },
    showMessage: { type: 'boolean', default: true }
  },
  edit: ({ attributes, setAttributes }) => {
    return (
      <div>
        <TextControl
          label="Message"
          value={attributes.message}
          onChange={(value) => setAttributes({ message: value })}
        />
        <ToggleControl
          label="Show Message"
          checked={attributes.showMessage}
          onChange={(value) => setAttributes({ showMessage: value })}
        />
        {attributes.showMessage && <p>{attributes.message}</p>}
      </div>
    );
  },
  save: ({ attributes }) => {
    return attributes.showMessage ? <p>{attributes.message}</p> : null;
  }
});
OutputSuccess
Important Notes

Always provide default values for attributes to avoid errors.

Use controls that match the attribute type for better user experience.

Remember to update attributes with setAttributes inside controls.

Summary

Block attributes hold the data or settings of a block.

Controls let users change these attributes in the editor.

Use attributes and controls together to make blocks interactive and customizable.

Practice

(1/5)
1. What is the main purpose of block attributes in WordPress block development?
easy
A. To store data or settings related to the block
B. To style the block with CSS
C. To add animations to the block
D. To create new blocks automatically

Solution

  1. Step 1: Understand block attributes role

    Block attributes hold the data or settings that define how a block behaves or looks. Differentiate from styling or automation: Attributes are not for styling or creating blocks automatically but for storing block-specific data.
  2. Final Answer:

    To store data or settings related to the block -> Option A
  3. Quick Check:

    Block attributes = store data/settings [OK]
Hint: Attributes hold block data/settings, not styles or animations [OK]
Common Mistakes:
  • Confusing attributes with CSS styling
  • Thinking attributes create blocks automatically
  • Assuming attributes control animations directly
2. Which of the following is the correct way to define a block attribute named title of type string in a block's attributes object?
easy
A. "title": { type: String }
B. "title": "string"
C. "title": { value: "string" }
D. "title": { type: "string" }

Solution

  1. Step 1: Recall attribute syntax in block.json or registerBlockType

    Attributes are defined as objects with a type property as a lowercase string. Compare options: \"title\": { type: \"string\" } uses { type: \"string\" } which is correct. \"title\": { type: String } uses String (capitalized) which is invalid.
  2. Final Answer:

    \"title\": { type: \"string\" } -> Option D
  3. Quick Check:

    Attribute type uses lowercase string \"string\" [OK]
Hint: Use { type: \"string\" } with lowercase type name [OK]
Common Mistakes:
  • Using capitalized String instead of \"string\"
  • Assigning attribute directly to a string
  • Using incorrect property like value instead of type
3. Given this block attribute and control code snippet:
const [content, setContent] = useState('Hello');

return (
   setContent(value)}
  />
);

What will happen when the user types "World" into the TextControl?
medium
A. An error occurs because useState is not allowed in blocks
B. The content state updates to "World" and the TextControl shows "World"
C. The content state remains "Hello" and TextControl does not change
D. The TextControl clears and shows an empty string

Solution

  1. Step 1: Understand useState and TextControl interaction

    The useState hook holds the current content. The TextControl shows this content and updates it on user input. Follow the onChange handler: When user types \"World\", onChange calls setContent(\"World\"), updating the state and re-rendering the control with new value.
  2. Final Answer:

    The content state updates to "World" and the TextControl shows "World" -> Option B
  3. Quick Check:

    Typing updates state and control value [OK]
Hint: State updates on change, control reflects new value [OK]
Common Mistakes:
  • Thinking state won't update without explicit save
  • Believing useState is disallowed in block edit
  • Assuming control clears on input
4. Consider this block attribute definition:
attributes: {
  count: { type: 'number', default: 0 }
}

And this control code:
 setAttributes({ count: value })}
/>

What is the likely cause if the control does not update the count attribute correctly when typing?
medium
A. The value prop should be a number, but onChange passes a string
B. The attribute type should be "string" instead of "number"
C. The default value must be set inside setAttributes
D. The control component NumberControl does not exist in WordPress

Solution

  1. Step 1: Check data types between attribute and control

    The attribute expects a number, but onChange often passes a string from input controls. Identify mismatch causing update failure: Passing a string instead of a number causes the attribute not to update properly because types differ.
  2. Final Answer:

    The value prop should be a number, but onChange passes a string -> Option A
  3. Quick Check:

    Type mismatch: onChange passes string to number attribute [OK]
Hint: Ensure onChange passes correct type matching attribute [OK]
Common Mistakes:
  • Ignoring type conversion from string to number
  • Changing attribute type unnecessarily
  • Assuming default value fixes runtime type issues
5. You want to create a block with a toggle control that updates a boolean attribute isActive. Which code snippet correctly defines the attribute and control to toggle this value?
hard
A.
attributes: { isActive: { type: 'boolean' } }

 setAttributes({ isActive: !attributes.isActive })}
/>
B.
attributes: { isActive: { type: 'string', default: 'false' } }

 setAttributes({ isActive: value })}
/>
C.
attributes: { isActive: { type: 'boolean', default: false } }

 setAttributes({ isActive: value })}
/>
D.
attributes: { isActive: { type: 'boolean', default: true } }

 setAttributes({ isActive: !value })}
/>

Solution

  1. Step 1: Define boolean attribute with default false

    attributes: { isActive: { type: 'boolean', default: false } }
    
     setAttributes({ isActive: value })}
    />
    correctly defines isActive as boolean with default false, which is standard for toggles. Use ToggleControl with checked and onChange: The checked prop uses the attribute value, and onChange updates the attribute with the new boolean value passed by the toggle.
  2. Final Answer:

    { isActive: { type: 'boolean', default: false } }, ToggleControl checked={attributes.isActive} onChange={(value) => setAttributes({ isActive: value })} -> Option C
  3. Quick Check:

    Boolean attribute with checked and direct onChange update [OK]
Hint: Use checked prop and boolean attribute with default false [OK]
Common Mistakes:
  • Using string type for boolean attribute
  • Using value instead of checked prop
  • Inverting value incorrectly in onChange
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard