Consider this WordPress block registration snippet. What will the block display in the editor?
wp.blocks.registerBlockType('myplugin/simple-block', { title: 'Simple Block', category: 'widgets', edit: () => wp.element.createElement('p', null, 'Hello Editor'), save: () => wp.element.createElement('p', null, 'Hello Frontend') });
Remember the difference between edit and save functions in block registration.
The edit function defines what appears in the editor. The save function defines the saved content for the frontend. So editor shows 'Hello Editor' and frontend shows 'Hello Frontend'.
Which code snippet correctly registers a block with an attribute content of type string?
Attributes must be defined with a type property as a string.
Attributes in block registration require an object with a type string. Option A correctly uses { type: 'string' }. Option A uses a string directly, which is invalid. Option A uses String (capitalized), which is incorrect. Option A uses 'text' which is not a valid type.
Given this code, why does the block fail to register?
wp.blocks.registerBlockType('myplugin/fail-block', { title: 'Fail Block', category: 'common', edit: () => wp.element.createElement('div', null, 'Fail'), save: () => wp.element.createElement('div', null, 'Fail') });
Check the commas between object properties carefully.
JavaScript objects require commas between properties. Missing the comma after title: 'Fail Block' causes a syntax error, so the block fails to register.
In this block, the user types text in a textarea. What will be the value of content attribute after typing 'Hi'?
wp.blocks.registerBlockType('myplugin/text-block', { attributes: { content: { type: 'string' } }, edit: (props) => { const onChange = (event) => props.setAttributes({ content: event.target.value }); return wp.element.createElement('textarea', { value: props.attributes.content || '', onChange }); }, save: (props) => wp.element.createElement('p', null, props.attributes.content) });
Typing triggers onChange which updates the attribute.
The onChange handler updates the content attribute with the textarea's value. Typing 'Hi' sets content to 'Hi'.
render_callback in PHP?Why do server-side rendering blocks in WordPress require a render_callback function in PHP?
Think about why some blocks need fresh data every time the page is viewed.
Server-side rendering blocks use render_callback to generate block HTML dynamically on the server. This allows the block to show up-to-date or dynamic content each time the page loads.