Blocks let you build content pieces easily in WordPress. They make editing simple and flexible without coding every time.
Block development basics in Wordpress
wp.blocks.registerBlockType('namespace/block-name', { title: 'Block Title', icon: 'smiley', category: 'common', edit: () => { return wp.element.createElement('p', null, 'Hello from the editor!'); }, save: () => { return wp.element.createElement('p', null, 'Hello from the saved content!'); }, });
registerBlockType is the main function to create a block.
edit defines what shows in the editor, save defines what is saved in the post content.
wp.blocks.registerBlockType('myplugin/simple-text', { title: 'Simple Text', icon: 'editor-paragraph', category: 'formatting', edit: () => wp.element.createElement('p', null, 'Type your text here'), save: () => wp.element.createElement('p', null, 'Type your text here'), });
wp.blocks.registerBlockType('myplugin/dynamic-message', { title: 'Dynamic Message', icon: 'megaphone', category: 'widgets', edit: () => { const message = 'Hello, WordPress!'; return wp.element.createElement('p', null, message); }, save: () => { return wp.element.createElement('p', null, 'Hello, WordPress!'); }, });
This block shows a friendly greeting both in the editor and on the published page. It uses the basic block structure with edit and save functions returning simple paragraphs.
wp.blocks.registerBlockType('myplugin/greeting-block', { title: 'Greeting Block', icon: 'smiley', category: 'widgets', edit: () => { return wp.element.createElement('p', null, 'Welcome to your custom block!'); }, save: () => { return wp.element.createElement('p', null, 'Welcome to your custom block!'); }, });
Always use unique namespaced block names like 'myplugin/block-name' to avoid conflicts.
The edit function runs in the editor and can use React features.
The save function returns the HTML saved in the post content and shown on the site.
Blocks let you create reusable content pieces in WordPress visually.
Use registerBlockType with edit and save functions to define blocks.
Blocks improve editing experience and make content flexible without coding each time.
Practice
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 AQuick Check:
Blocks = reusable visual content [OK]
- Confusing blocks with PHP theme coding
- Thinking blocks manage users or database
- Assuming blocks are only for styling
Solution
Step 1: Recall the standard WordPress block registration function
The official function to register blocks isregisterBlockType.Step 2: Verify other options are incorrect
Functions likecreateBlockType,addBlock, andinitBlockdo not exist in WordPress block API.Final Answer:
registerBlockType() -> Option DQuick Check:
Register blocks = registerBlockType() [OK]
- Using createBlockType() which is not a WordPress function
- Confusing with addBlock() or initBlock() which don't exist
- Misspelling the function name
registerBlockType('myplugin/example', {
edit: () => {
return Hello, WordPress!
;
},
save: () => {
return Hello, WordPress!
;
}
});Solution
Step 1: Analyze the edit function output
The edit function returns a paragraph with 'Hello, WordPress!' which shows in the editor.Step 2: Analyze the save function output
The save function returns the same paragraph, so saved content also shows 'Hello, WordPress!'.Final Answer:
A paragraph with text 'Hello, WordPress!' in both editor and saved content -> Option AQuick Check:
Edit and save return same paragraph = A paragraph with text 'Hello, WordPress!' in both editor and saved content [OK]
- Thinking edit content differs from saved content
- Assuming syntax error due to JSX
- Confusing block output with empty or error
registerBlockType('myplugin/test', {
edit: () => {
return Test Block;
},
save: () => {
return div>Test Block;
}
});Solution
Step 1: Check the save function JSX syntax
The save function returnsdiv>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 BQuick Check:
JSX tags must start with < [OK]
- Ignoring missing < in JSX tags
- Thinking function names or block names are wrong
- Overlooking return statements
Solution
Step 1: Understand attribute usage in blocks
Attributes must be declared inregisterBlockTypeand accessed viaprops.attributesin 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 CQuick Check:
Attributes declared and used properly = Define attributes in registerBlockType and use props.attributes.name in edit and save [OK]
- Using global variables instead of attributes
- Hardcoding values losing flexibility
- Not using attributes in save function
Ready to prove it?
Pass the test and this pattern moves unknown
