📖 Scenario: You are building a WordPress site for a small business. You want to add extra information fields to the post editor screen so the content creators can add special details easily.
🎯 Goal: Build a custom meta box in the WordPress post editor that allows users to enter a subtitle for each post.
📋 What You'll Learn
Create a function to register a meta box with the ID subtitle_meta_box and title Post Subtitle
Add the meta box to the post post type
Create a callback function to display a text input field with the name post_subtitle
Save the subtitle value when the post is saved
💡 Why This Matters
🌍 Real World
Custom meta boxes let WordPress site owners add extra fields to posts or pages, making content management easier and more tailored to their needs.
💼 Career
Knowing how to create and save custom meta boxes is a common task for WordPress developers customizing themes and plugins.
Progress0 / 4 steps
1
Register the Meta Box
Write a function called add_subtitle_meta_box that uses add_meta_box to add a meta box with ID subtitle_meta_box, title Post Subtitle, callback subtitle_meta_box_callback, and screen post. Then hook this function to add_meta_boxes action.
Wordpress
Hint
Use add_meta_box inside your function and hook it with add_action.
2
Create the Meta Box Callback
Write the function subtitle_meta_box_callback that accepts the $post object. Inside, retrieve the saved meta value with get_post_meta using key post_subtitle. Then output a text input field with name post_subtitle and value set to the retrieved meta value.
Wordpress
Hint
Use get_post_meta to get the saved subtitle and output an input field with that value.
3
Save the Meta Box Data
Write a function called save_subtitle_meta_box_data that accepts $post_id. Inside, check if post_subtitle is set in $_POST. If yes, sanitize it with sanitize_text_field and save it using update_post_meta with key post_subtitle. Hook this function to save_post action.
Wordpress
Hint
Check if post_subtitle is in $_POST, sanitize it, then save with update_post_meta.
4
Add Nonce for Security
In the subtitle_meta_box_callback function, add a nonce field with wp_nonce_field using action save_subtitle_meta_box and name subtitle_meta_box_nonce. Then, in save_subtitle_meta_box_data, verify the nonce from $_POST['subtitle_meta_box_nonce'] using wp_verify_nonce with the same action. If nonce verification fails, return early without saving.
Wordpress
Hint
Use wp_nonce_field in the callback and verify it in the save function with wp_verify_nonce.
Practice
(1/5)
1. What is the main purpose of adding a custom meta box in WordPress?
easy
A. To change the theme colors automatically
B. To create new user roles
C. To add extra input fields on the post edit screen
D. To optimize the website speed
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 C
Quick Check:
Custom meta boxes = extra input fields [OK]
Hint: Meta boxes add fields on edit screens [OK]
Common Mistakes:
Confusing meta boxes with theme or user settings
Thinking meta boxes change site speed
Assuming meta boxes create user roles
2. Which function is used to add a custom meta box in WordPress?
easy
A. add_meta_box()
B. register_meta_box()
C. add_custom_box()
D. create_meta_box()
Solution
Step 1: Recall WordPress function for meta boxes
The official function to add meta boxes is add_meta_box().
Step 2: Verify other options
Other options are not valid WordPress functions for meta boxes.
Final Answer:
add_meta_box() -> Option A
Quick Check:
Use add_meta_box() to add meta boxes [OK]
Hint: Remember exact function name: add_meta_box() [OK]
Common Mistakes:
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
3. Given this code snippet, what will be the output on the post edit screen?
B. It uses the wrong hook; should be 'add_meta_box'
C. It updates meta with wrong function; should use add_post_meta
D. It lacks a nonce check for security
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 D
Quick Check:
Always verify nonce before saving meta data [OK]
Hint: Always check nonce before saving meta data [OK]
Common Mistakes:
Skipping nonce verification
Using wrong hook for saving data
Confusing update_post_meta with add_post_meta
5. You want to add a custom meta box that only appears on pages (not posts) and saves a checkbox value safely. Which steps are correct to implement this?
hard
A. Use add_meta_box with 'page' screen, output checkbox in callback, save with save_post hook including nonce and permission checks
B. Use add_meta_box with 'post' screen, output checkbox, save without nonce check
C. Use add_meta_box with 'page' screen, output text input, save with save_post hook but no permission check
D. Use add_meta_box with 'post' screen, output checkbox, save with add_post_meta function
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 A
Quick Check:
Screen='page', checkbox output, secure save with nonce [OK]
Hint: Match screen type and add nonce checks when saving [OK]
Common Mistakes:
Using wrong screen type ('post' instead of 'page')
Skipping nonce or permission checks
Saving checkbox as text input
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.