0
0
Wordpressframework~20 mins

Block registration in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Block Registration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this block registration code?

Consider this WordPress block registration snippet. What will the block display in the editor?

Wordpress
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')
});
ABoth editor and frontend show 'Hello Frontend'.
BThe editor shows a paragraph with text 'Hello Editor', and the frontend shows 'Hello Frontend'.
CBoth editor and frontend show 'Hello Editor'.
DThe editor shows 'Hello Frontend', and the frontend shows 'Hello Editor'.
Attempts:
2 left
💡 Hint

Remember the difference between edit and save functions in block registration.

📝 Syntax
intermediate
2:00remaining
Which option correctly registers a block with attributes?

Which code snippet correctly registers a block with an attribute content of type string?

A
wp.blocks.registerBlockType('myplugin/attr-block', {
  attributes: { content: { type: 'string' } },
  edit: (props) => wp.element.createElement('p', null, props.attributes.content),
  save: (props) => wp.element.createElement('p', null, props.attributes.content)
});
B
wp.blocks.registerBlockType('myplugin/attr-block', {
  attributes: { content: 'string' },
  edit: (props) => wp.element.createElement('p', null, props.content),
  save: (props) => wp.element.createElement('p', null, props.content)
});
C
wp.blocks.registerBlockType('myplugin/attr-block', {
  attributes: { content: { type: String } },
  edit: (props) => wp.element.createElement('p', null, props.attributes.content),
  save: (props) => wp.element.createElement('p', null, props.attributes.content)
});
D
wp.blocks.registerBlockType('myplugin/attr-block', {
  attributes: { content: { type: 'text' } },
  edit: (props) => wp.element.createElement('p', null, props.attributes.content),
  save: (props) => wp.element.createElement('p', null, props.attributes.content)
});
Attempts:
2 left
💡 Hint

Attributes must be defined with a type property as a string.

🔧 Debug
advanced
2:00remaining
Why does this block fail to register?

Given this code, why does the block fail to register?

Wordpress
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')
});
AThe category 'common' is invalid and causes registration failure.
BThe edit function must return a string, not a React element.
CMissing comma after the title property causes a syntax error.
DThe save function is missing a required attribute property.
Attempts:
2 left
💡 Hint

Check the commas between object properties carefully.

state_output
advanced
2:00remaining
What is the value of the attribute after typing in this block?

In this block, the user types text in a textarea. What will be the value of content attribute after typing 'Hi'?

Wordpress
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)
});
AAn empty string
Bundefined
Cnull
D'Hi'
Attempts:
2 left
💡 Hint

Typing triggers onChange which updates the attribute.

🧠 Conceptual
expert
3:00remaining
Which option best explains why server-side rendering blocks use render_callback in PHP?

Why do server-side rendering blocks in WordPress require a render_callback function in PHP?

ABecause the block output depends on dynamic data that must be generated on the server each time the page loads.
BBecause JavaScript cannot render blocks in the editor without PHP functions.
CBecause <code>render_callback</code> caches the block output to improve performance.
DBecause <code>render_callback</code> is required to register block scripts and styles.
Attempts:
2 left
💡 Hint

Think about why some blocks need fresh data every time the page is viewed.