Custom meta boxes let you add extra fields to WordPress posts or pages. This helps you store and show more information easily.
Custom meta boxes in Wordpress
<?php
function add_custom_meta_box() {
add_meta_box(
'custom_meta_box_id', // Unique ID
'Custom Meta Box Title', // Box title
'custom_meta_box_callback', // Callback function
'post', // Screen to show on (post, page, custom post type)
'normal', // Context (normal, side, advanced)
'high' // Priority
);
}
add_action('add_meta_boxes', 'add_custom_meta_box');
function custom_meta_box_callback($post) {
// Add nonce for security
wp_nonce_field('custom_meta_box_nonce_action', 'custom_meta_box_nonce');
// Get existing value if any
$value = get_post_meta($post->ID, '_custom_meta_key', true);
// Display input field
echo '<label for="custom_field">Custom Field:</label>';
echo '<input type="text" id="custom_field" name="custom_field" value="' . esc_attr($value) . '" size="25" />';
}
function save_custom_meta_box_data($post_id) {
// Check nonce
if (!isset($_POST['custom_meta_box_nonce']) || !wp_verify_nonce($_POST['custom_meta_box_nonce'], 'custom_meta_box_nonce_action')) {
return;
}
// Check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check user permissions
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Save or delete meta
if (isset($_POST['custom_field'])) {
update_post_meta($post_id, '_custom_meta_key', sanitize_text_field($_POST['custom_field']));
} else {
delete_post_meta($post_id, '_custom_meta_key');
}
}
add_action('save_post', 'save_custom_meta_box_data');The add_meta_box function adds the box to the edit screen.
The callback function outputs the HTML for the box content.
<?php
// Add meta box to pages instead of posts
function add_custom_meta_box_page() {
add_meta_box('custom_meta_box_id', 'Page Meta Box', 'custom_meta_box_callback', 'page');
}
add_action('add_meta_boxes', 'add_custom_meta_box_page');<?php // Use a textarea instead of input function custom_meta_box_callback($post) { wp_nonce_field('custom_meta_box_nonce_action', 'custom_meta_box_nonce'); $value = get_post_meta($post->ID, '_custom_meta_key', true); echo '<label for="custom_field">Description:</label>'; echo '<textarea id="custom_field" name="custom_field" rows="4" cols="30">' . esc_textarea($value) . '</textarea>'; }
This code adds a meta box titled "Favorite Color" on the side of the post edit screen. It shows a text input where you can type a color. When you save the post, it stores the color as post meta data.
<?php
// Add a custom meta box to posts
function add_custom_meta_box() {
add_meta_box(
'custom_meta_box_id',
'Favorite Color',
'custom_meta_box_callback',
'post',
'side',
'default'
);
}
add_action('add_meta_boxes', 'add_custom_meta_box');
function custom_meta_box_callback($post) {
wp_nonce_field('custom_meta_box_nonce_action', 'custom_meta_box_nonce');
$value = get_post_meta($post->ID, '_favorite_color', true);
echo '<label for="favorite_color">Favorite Color:</label><br />';
echo '<input type="text" id="favorite_color" name="favorite_color" value="' . esc_attr($value) . '" size="20" />';
}
function save_custom_meta_box_data($post_id) {
if (!isset($_POST['custom_meta_box_nonce']) || !wp_verify_nonce($_POST['custom_meta_box_nonce'], 'custom_meta_box_nonce_action')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_POST['favorite_color'])) {
update_post_meta($post_id, '_favorite_color', sanitize_text_field($_POST['favorite_color']));
} else {
delete_post_meta($post_id, '_favorite_color');
}
}
add_action('save_post', 'save_custom_meta_box_data');Always use wp_nonce_field and check nonce to secure your meta box.
Use sanitize_text_field or similar functions to clean user input before saving.
Remember to check user permissions to avoid unauthorized changes.
Custom meta boxes add extra fields to WordPress edit screens.
Use add_meta_box to create the box and a callback to show fields.
Save data securely using save_post action with nonce and permission checks.
Practice
Solution
Step 1: Understand what meta boxes do
Meta boxes provide extra areas on the post edit screen for input.Step 2: Identify the purpose of custom meta boxes
They allow adding custom fields to collect extra data for posts or pages.Final Answer:
To add extra input fields on the post edit screen -> Option CQuick Check:
Custom meta boxes = extra input fields [OK]
- Confusing meta boxes with theme or user settings
- Thinking meta boxes change site speed
- Assuming meta boxes create user roles
Solution
Step 1: Recall WordPress function for meta boxes
The official function to add meta boxes isadd_meta_box().Step 2: Verify other options
Other options are not valid WordPress functions for meta boxes.Final Answer:
add_meta_box() -> Option AQuick Check:
Use add_meta_box() to add meta boxes [OK]
- Using incorrect function names like add_custom_box()
- Confusing with register_meta_box() which doesn't exist
- Trying to create meta boxes without this function
add_action('add_meta_boxes', function() {
add_meta_box('my_box', 'My Box', function() {
echo '<input type="text" name="my_field" value="Hello" />';
}, 'post');
});Solution
Step 1: Analyze add_meta_box parameters
The ID is 'my_box', title is 'My Box', callback outputs a text input with value 'Hello', and screen is 'post'.Step 2: Understand the callback output
The callback prints an input box of type text with name 'my_field' and value 'Hello'.Final Answer:
A text input box labeled 'My Box' with 'Hello' inside -> Option BQuick Check:
Callback outputs text input with value 'Hello' [OK]
- Thinking callback needs parameters to work
- Assuming 'post' is invalid screen
- Confusing input types (text vs checkbox)
add_action('save_post', function($post_id) {
update_post_meta($post_id, 'my_field', $_POST['my_field']);
});Solution
Step 1: Check security best practices
Saving meta box data requires verifying a nonce to prevent unauthorized saves.Step 2: Identify missing nonce check
The code updates post meta directly without checking nonce or user permissions.Final Answer:
It lacks a nonce check for security -> Option DQuick Check:
Always verify nonce before saving meta data [OK]
- Skipping nonce verification
- Using wrong hook for saving data
- Confusing update_post_meta with add_post_meta
Solution
Step 1: Choose correct screen and output
To show on pages only, use 'page' as screen in add_meta_box and output a checkbox input in the callback.Step 2: Save data securely
Use the 'save_post' hook to save data, but include nonce verification and user permission checks to ensure security.Final Answer:
Use add_meta_box with 'page' screen, output checkbox in callback, save with save_post hook including nonce and permission checks -> Option AQuick Check:
Screen='page', checkbox output, secure save with nonce [OK]
- Using wrong screen type ('post' instead of 'page')
- Skipping nonce or permission checks
- Saving checkbox as text input
Ready to prove it?
Pass the test and this pattern moves unknown
