Block registration lets you add new blocks to the WordPress editor. This helps you create custom content pieces easily.
Block registration in Wordpress
registerBlockType('namespace/block-name', { title: 'Block Title', icon: 'smiley', category: 'common', edit: () => { return <p>Hello from the block editor!</p>; }, save: () => { return <p>Hello from the saved content!</p>; }, });
The first argument is a unique name with a namespace and block name separated by a slash.
The edit function defines what appears in the editor, and save defines what is saved in the post content.
registerBlockType('myplugin/alert', { title: 'Alert Box', icon: 'warning', category: 'widgets', edit: () => <div style={{backgroundColor: 'yellow', padding: '10px'}}>Alert in editor</div>, save: () => <div style={{backgroundColor: 'yellow', padding: '10px'}}>Alert on page</div>, });
registerBlockType('myplugin/simple-text', { title: 'Simple Text', icon: 'text', category: 'text', edit: () => <p>Type your text here</p>, save: () => <p>Type your text here</p>, });
This block shows a button in the editor that counts clicks. The saved content is a simple button text because saved content can't have dynamic state.
import { registerBlockType } from '@wordpress/blocks'; import { useState } from '@wordpress/element'; registerBlockType('myplugin/click-counter', { title: 'Click Counter', icon: 'smiley', category: 'widgets', edit: () => { const [count, setCount] = useState(0); return ( <button onClick={() => setCount(count + 1)} aria-label="Click counter button"> Clicked {count} times </button> ); }, save: () => { return <button>Click counter saved block</button>; }, });
Blocks must have a unique name with a namespace to avoid conflicts.
The edit function can use React hooks like useState for interactivity in the editor.
The save function returns static content that is saved in the post and shown on the site.
Block registration adds custom blocks to the WordPress editor.
Use registerBlockType with a unique name and provide edit and save functions.
Blocks help create rich, reusable content easily for editors and visitors.
Practice
registerBlockType in WordPress block development?Solution
Step 1: Understand the role of
This function is used specifically to add custom blocks to the WordPress block editor.registerBlockTypeStep 2: Compare with other WordPress features
Registering themes, user roles, or plugins are done by different functions, notregisterBlockType.Final Answer:
To add a new custom block to the WordPress editor -> Option AQuick Check:
Block registration = add custom block [OK]
- Confusing block registration with theme or plugin registration
- Thinking it creates user roles
- Assuming it installs plugins automatically
Solution
Step 1: Identify the correct function name and parameters
The correct function isregisterBlockTypeand it requires a unique block name and an object witheditandsavefunctions.Step 2: Check the block name format and object completeness
The block name must be a string with a namespace and name separated by a slash, like 'myplugin/example'. The object must include botheditandsavefunctions.Final Answer:
registerBlockType('myplugin/example', { edit: () => {}, save: () => {} }); -> Option CQuick Check:
Correct function and full object = registerBlockType('myplugin/example', { edit: () => {}, save: () => {} }); [OK]
- Using registerBlock instead of registerBlockType
- Using underscore instead of slash in block name
- Omitting edit or save functions
registerBlockType('myplugin/simple', {
edit: () => {
return wp.element.createElement('p', null, 'Hello Editor');
},
save: () => {
return wp.element.createElement('p', null, 'Hello Saved');
}
});Solution
Step 1: Understand the difference between edit and save functions
Theeditfunction controls what the editor shows while editing. Thesavefunction controls what HTML is saved and shown on the front end.Step 2: Identify the saved output
Thesavefunction returns a paragraph with text 'Hello Saved', so this is the content saved in the post and shown to visitors.Final Answer:
<p>Hello Saved</p> -> Option BQuick Check:
Saved content = save() output = <p>Hello Saved</p> [OK]
- Confusing edit() output with saved content
- Ignoring that save() returns HTML elements
- Assuming edit() content is saved
registerBlockType('myplugin/test', {
edit() {
return <p>Edit Block</p>
},
save() {
return <p>Save Block</p>
}
});Solution
Step 1: Check the syntax used in edit and save functions
The code uses JSX syntax like <p>Edit Block</p> directly, which requires React and Babel setup to compile.Step 2: Identify environment requirements
If JSX is used without proper setup, this code will cause syntax errors. The code should usewp.element.createElementor have JSX compiled.Final Answer:
JSX syntax used without React or Babel setup -> Option AQuick Check:
JSX needs compilation = JSX syntax used without React or Babel setup [OK]
- Assuming JSX works without setup
- Thinking semicolons are mandatory after return
- Believing edit/save must be arrow functions
Solution
Step 1: Define attributes properly for data handling
To handle a list, store it as an array attribute so it can be easily manipulated inedit.Step 2: Implement edit and save functions correctly
editupdates the array attribute.savejoins the array into a comma-separated string inside a paragraph tag for saving as HTML.Final Answer:
Use attributes to store array, edit to update, save to join as string -> Option DQuick Check:
Array attribute + join in save = Useattributesto store an array,editto update it, andsaveto join array into a string inside a <p> tag. [OK]
- Storing list as string and trying to output array directly
- Ignoring attributes and hardcoding data
- Saving data as JSON instead of HTML
Ready to prove it?
Pass the test and this pattern moves unknown
