📖 Scenario: You are creating a simple custom block for the WordPress block editor. This block will display a greeting message that can be customized by the user.
🎯 Goal: Build a basic WordPress block that registers a block named greeting-block and displays a customizable greeting message inside the editor and on the site.
📋 What You'll Learn
Create a block registration with registerBlockType using the name myplugin/greeting-block.
Add an attribute called message of type string with a default value.
Use the edit function to show a text input for the message attribute.
Use the save function to output the greeting message inside a p tag.
💡 Why This Matters
🌍 Real World
Custom blocks let WordPress users add tailored content easily in the block editor, improving site flexibility and user experience.
💼 Career
Knowing block development is essential for WordPress developers creating themes and plugins that extend editor capabilities.
Progress0 / 4 steps
1
Create block registration with initial attributes
Write the code to register a block using registerBlockType with the name myplugin/greeting-block. Add an attribute called message of type string with the default value 'Hello, world!'.
Wordpress
Hint
Use wp.blocks.registerBlockType with the block name and an attributes object containing message.
2
Add edit function with text input for message
Add an edit function to the block registration that uses props.attributes.message and props.setAttributes. Inside, return a TextControl component from wp.components that updates the message attribute when changed.
Wordpress
Hint
Use TextControl from wp.components inside the edit function to let users change the message.
3
Add save function to output the message
Add a save function to the block registration that returns a p element containing the message attribute value.
Wordpress
Hint
The save function should return a p tag with the message text using wp.element.createElement.
4
Complete block registration with all parts
Ensure the block registration includes the attributes, edit, and save functions exactly as specified. The block name must be myplugin/greeting-block. The edit function uses TextControl to edit message. The save function outputs the message inside a p tag.
Wordpress
Hint
Double-check that all parts are included and the block name is correct.
Practice
(1/5)
1. What is the main purpose of creating blocks in WordPress block development?
easy
A. To create reusable content pieces that users can add visually
B. To write PHP code for custom themes
C. To manage user roles and permissions
D. To optimize database queries
Solution
Step 1: Understand the role of blocks in WordPress
Blocks are designed to let users add content pieces easily without coding each time.
Step 2: Identify the main benefit of blocks
Blocks are reusable and visually manageable content units improving editing experience.
Final Answer:
To create reusable content pieces that users can add visually -> Option A
Quick Check:
Blocks = reusable visual content [OK]
Hint: Blocks are for reusable visual content pieces [OK]
Common Mistakes:
Confusing blocks with PHP theme coding
Thinking blocks manage users or database
Assuming blocks are only for styling
2. Which function is used to register a new block type in WordPress block development?
easy
A. createBlockType()
B. initBlock()
C. addBlock()
D. registerBlockType()
Solution
Step 1: Recall the standard WordPress block registration function
The official function to register blocks is registerBlockType.
Step 2: Verify other options are incorrect
Functions like createBlockType, addBlock, and initBlock do not exist in WordPress block API.
The save function returns div>Test Block; which is missing the opening < for the div tag.
Step 2: Verify other parts are correct
The edit function correctly returns JSX with <div>, the function name is correct, and block name format is valid.
Final Answer:
Missing opening < in save function JSX -> Option B
Quick Check:
JSX tags must start with < [OK]
Hint: Check JSX tags start with < and end with > [OK]
Common Mistakes:
Ignoring missing < in JSX tags
Thinking function names or block names are wrong
Overlooking return statements
5. You want to create a block that shows a dynamic greeting with a user name passed as an attribute. Which approach correctly uses attributes in block development?
hard
A. Hardcode the user name inside the save function JSX
B. Use a global variable for the name inside edit and save functions
C. Define attributes in registerBlockType and use props.attributes.name in edit and save
D. Only define attributes in edit function, ignore save function
Solution
Step 1: Understand attribute usage in blocks
Attributes must be declared in registerBlockType and accessed via props.attributes in both edit and save functions.
Step 2: Evaluate other options
Using global variables or hardcoding breaks reusability and dynamic behavior; ignoring attributes in save causes data loss.
Final Answer:
Define attributes in registerBlockType and use props.attributes.name in edit and save -> Option C
Quick Check:
Attributes declared and used properly = Define attributes in registerBlockType and use props.attributes.name in edit and save [OK]
Hint: Declare and use attributes via props in edit and save [OK]
Common Mistakes:
Using global variables instead of attributes
Hardcoding values losing flexibility
Not using attributes in save function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.