0
0
Wordpressframework~20 mins

Custom meta boxes in Wordpress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Meta Boxes 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 WordPress custom meta box code?

Consider this code snippet that adds a custom meta box to the post editor:

function add_custom_meta_box() {
  add_meta_box(
    'my_meta_box',
    'My Meta Box',
    'render_my_meta_box',
    'post',
    'side',
    'default'
  );
}
add_action('add_meta_boxes', 'add_custom_meta_box');

function render_my_meta_box($post) {
  echo '<label for="my_field">Enter text:</label>';
  echo '<input type="text" id="my_field" name="my_field" value="" />';
}

What will the user see in the post editor screen?

Wordpress
function add_custom_meta_box() {
  add_meta_box(
    'my_meta_box',
    'My Meta Box',
    'render_my_meta_box',
    'post',
    'side',
    'default'
  );
}
add_action('add_meta_boxes', 'add_custom_meta_box');

function render_my_meta_box($post) {
  echo '<label for="my_field">Enter text:</label>';
  echo '<input type="text" id="my_field" name="my_field" value="" />';
}
AA new box titled 'My Meta Box' appears in the sidebar with a label 'Enter text:' and an empty text input field.
BA new box titled 'My Meta Box' appears below the post content with a label and a dropdown menu.
CNo new box appears because the add_meta_box function is missing a required parameter.
DA fatal error occurs because the render_my_meta_box function is not hooked properly.
Attempts:
2 left
💡 Hint

Look at the parameters of add_meta_box and the output of the callback function.

state_output
intermediate
2:00remaining
What is the saved meta value after this code runs?

This code saves a custom meta field when a post is saved:

function save_my_meta_box_data($post_id) {
  if (!isset($_POST['my_field'])) return;
  update_post_meta($post_id, 'my_field', sanitize_text_field($_POST['my_field']));
}
add_action('save_post', 'save_my_meta_box_data');

If the user enters 'Hello World' in the meta box input and saves the post, what will be stored in the post meta?

Wordpress
function save_my_meta_box_data($post_id) {
  if (!isset($_POST['my_field'])) return;
  update_post_meta($post_id, 'my_field', sanitize_text_field($_POST['my_field']));
}
add_action('save_post', 'save_my_meta_box_data');
AA PHP error because $_POST['my_field'] is not checked before use
BAn empty string because the input value is not saved automatically
CThe string 'my_field' because the key is saved instead of the value
D'Hello World' (the exact text entered, sanitized to remove harmful code)
Attempts:
2 left
💡 Hint

Check how update_post_meta and sanitize_text_field work.

🔧 Debug
advanced
2:00remaining
Why does this custom meta box not appear on the post editor?

Look at this code snippet:

function add_custom_box() {
  add_meta_box(
    'custom_box',
    'Custom Box',
    'custom_box_callback',
    'page',
    'normal',
    'high'
  );
}
add_action('add_meta_boxes_post', 'add_custom_box');

function custom_box_callback($post) {
  echo '<p>Custom content here</p>';
}

The developer expects the box to appear on the post editor screen, but it does not. Why?

Wordpress
function add_custom_box() {
  add_meta_box(
    'custom_box',
    'Custom Box',
    'custom_box_callback',
    'page',
    'normal',
    'high'
  );
}
add_action('add_meta_boxes_post', 'add_custom_box');

function custom_box_callback($post) {
  echo '<p>Custom content here</p>';
}
AThe priority 'high' is invalid and prevents the box from showing.
BThe callback function name is misspelled, causing the box not to render.
CThe meta box is registered for 'page' post type but hooked to 'add_meta_boxes_post', so it never runs for posts.
DThe meta box ID 'custom_box' conflicts with a core WordPress box, hiding it.
Attempts:
2 left
💡 Hint

Check the post type parameter and the hook name carefully.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this custom meta box code?

Identify which code snippet will cause a syntax error when adding a custom meta box:

Aadd_meta_box('box4', 'Box 4', 'callback_func', 'post', 'side', 'low');
Badd_meta_box('box3', 'Box 3', 'callback_func', 'post', 'normal' 'high');
Cadd_meta_box('box1', 'Box 1', 'callback_func', 'post', 'side', 'default');
Dadd_meta_box('box2', 'Box 2', callback_func, 'post', 'normal', 'high');
Attempts:
2 left
💡 Hint

Look for missing commas between parameters.

🧠 Conceptual
expert
2:00remaining
Which option best explains why nonce verification is important in saving custom meta box data?

When saving data from a custom meta box, developers often use a nonce field and verify it before saving. Why is this step important?

ATo prevent unauthorized users or scripts from submitting data and protect against Cross-Site Request Forgery (CSRF) attacks.
BTo speed up the saving process by caching the meta box data temporarily.
CTo automatically sanitize all input data without extra code.
DTo ensure the meta box appears only for logged-in users.
Attempts:
2 left
💡 Hint

Think about security risks when saving data from forms.