Bird
Raised Fist0
Wordpressframework~20 mins

Block registration in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of registerBlockType in WordPress block development?
easy
A. To add a new custom block to the WordPress editor
B. To register a new theme for WordPress
C. To create a new user role in WordPress
D. To install a plugin automatically

Solution

  1. Step 1: Understand the role of registerBlockType

    This function is used specifically to add custom blocks to the WordPress block editor.
  2. Step 2: Compare with other WordPress features

    Registering themes, user roles, or plugins are done by different functions, not registerBlockType.
  3. Final Answer:

    To add a new custom block to the WordPress editor -> Option A
  4. Quick Check:

    Block registration = add custom block [OK]
Hint: Remember: registerBlockType = add custom block [OK]
Common Mistakes:
  • Confusing block registration with theme or plugin registration
  • Thinking it creates user roles
  • Assuming it installs plugins automatically
2. Which of the following is the correct way to start registering a block named "myplugin/example" in JavaScript?
easy
A. registerBlockType('myplugin_example', { edit: () => {}, save: () => {} });
B. registerBlock('myplugin/example', { edit: () => {}, save: () => {} });
C. registerBlockType('myplugin/example', { edit: () => {}, save: () => {} });
D. registerBlockType('myplugin/example');

Solution

  1. Step 1: Identify the correct function name and parameters

    The correct function is registerBlockType and it requires a unique block name and an object with edit and save functions.
  2. 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 both edit and save functions.
  3. Final Answer:

    registerBlockType('myplugin/example', { edit: () => {}, save: () => {} }); -> Option C
  4. Quick Check:

    Correct function and full object = registerBlockType('myplugin/example', { edit: () => {}, save: () => {} }); [OK]
Hint: Use registerBlockType with namespace/name and edit/save [OK]
Common Mistakes:
  • Using registerBlock instead of registerBlockType
  • Using underscore instead of slash in block name
  • Omitting edit or save functions
3. Given this block registration code snippet, what will be the saved content output in the post?
registerBlockType('myplugin/simple', {
  edit: () => {
    return wp.element.createElement('p', null, 'Hello Editor');
  },
  save: () => {
    return wp.element.createElement('p', null, 'Hello Saved');
  }
});
medium
A. <p>Hello Editor</p>
B. <p>Hello Saved</p>
C. Hello Editor
D. Hello Saved

Solution

  1. Step 1: Understand the difference between edit and save functions

    The edit function controls what the editor shows while editing. The save function controls what HTML is saved and shown on the front end.
  2. Step 2: Identify the saved output

    The save function returns a paragraph with text 'Hello Saved', so this is the content saved in the post and shown to visitors.
  3. Final Answer:

    <p>Hello Saved</p> -> Option B
  4. Quick Check:

    Saved content = save() output = <p>Hello Saved</p> [OK]
Hint: Remember: save() output is what visitors see [OK]
Common Mistakes:
  • Confusing edit() output with saved content
  • Ignoring that save() returns HTML elements
  • Assuming edit() content is saved
4. Identify the error in this block registration code:
registerBlockType('myplugin/test', {
  edit() {
    return <p>Edit Block</p>
  },
  save() {
    return <p>Save Block</p>
  }
});
medium
A. JSX syntax used without React or Babel setup
B. Block name missing namespace
C. Missing semicolons after return statements
D. edit and save functions must be arrow functions

Solution

  1. 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.
  2. Step 2: Identify environment requirements

    If JSX is used without proper setup, this code will cause syntax errors. The code should use wp.element.createElement or have JSX compiled.
  3. Final Answer:

    JSX syntax used without React or Babel setup -> Option A
  4. Quick Check:

    JSX needs compilation = JSX syntax used without React or Babel setup [OK]
Hint: JSX needs React/Babel; otherwise use createElement [OK]
Common Mistakes:
  • Assuming JSX works without setup
  • Thinking semicolons are mandatory after return
  • Believing edit/save must be arrow functions
5. You want to register a block that shows a list of items passed as an attribute and saves them as a comma-separated string. Which approach correctly registers this block?
hard
A. Store the list as a string attribute, use edit to split it into an array, and save to output the array directly.
B. Store the list as an object attribute, use edit to convert it to string, and save to output JSON.
C. Use no attributes, hardcode the list in save, and ignore edit.
D. Use attributes to store an array, edit to update it, and save to join array into a string inside a <p> tag.

Solution

  1. Step 1: Define attributes properly for data handling

    To handle a list, store it as an array attribute so it can be easily manipulated in edit.
  2. Step 2: Implement edit and save functions correctly

    edit updates the array attribute. save joins the array into a comma-separated string inside a paragraph tag for saving as HTML.
  3. Final Answer:

    Use attributes to store array, edit to update, save to join as string -> Option D
  4. Quick Check:

    Array attribute + join in save = Use attributes to store an array, edit to update it, and save to join array into a string inside a <p> tag. [OK]
Hint: Store list as array attribute, join in save() output [OK]
Common Mistakes:
  • Storing list as string and trying to output array directly
  • Ignoring attributes and hardcoding data
  • Saving data as JSON instead of HTML
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard