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?
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="" />';
}Look at the parameters of add_meta_box and the output of the callback function.
The add_meta_box function adds a box titled 'My Meta Box' to the 'post' edit screen in the sidebar ('side' context). The callback render_my_meta_box outputs a label and an empty text input. So the user sees this box with the label and input field in the sidebar.
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?
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');Check how update_post_meta and sanitize_text_field work.
The function checks if $_POST['my_field'] exists, then sanitizes the input text and saves it as post meta with key 'my_field'. So the exact text entered, cleaned of harmful code, is saved.
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?
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>';
}Check the post type parameter and the hook name carefully.
The add_meta_box call targets the 'page' post type, but the hook used is add_meta_boxes_post, which runs only for 'post' post type. Since the hook and post type don't match, the box does not appear on the post editor.
Identify which code snippet will cause a syntax error when adding a custom meta box:
Look for missing commas between parameters.
Option B is missing a comma between 'normal' and 'high', causing a syntax error. All other options have correct syntax.
When saving data from a custom meta box, developers often use a nonce field and verify it before saving. Why is this step important?
Think about security risks when saving data from forms.
Nonce verification helps confirm that the data submission is intentional and from a valid source, protecting against CSRF attacks. It does not speed saving, sanitize data automatically, or control visibility.