Tuesday, March 18, 2025
•4 min read
How to Add Custom Meta Boxes in WordPress Without a Plugin
Need a way to attach extra info to your posts — like a subtitle, custom note, or rating? Meta boxes are the perfect solution. In this guide, you’ll learn how to create your own custom meta boxes in WordPress using just a few lines of PHP, no plugins required.
Step 1: Understand What Meta Boxes Are
Meta boxes are the draggable panels you see in the WordPress post editor — like Categories, Tags, or Featured Image. They’re designed to store extra data related to a post. With just a bit of code, you can add your own boxes to hold custom fields such as subtitles, external links, or ratings.
Step 2: Register a Custom Meta Box
To create a meta box, use WordPress’s built-in `add_meta_box()` function inside your theme’s `functions.php` file. Here’s an example that adds a custom meta box to post edit screens:
1function my_custom_meta_box() {
2 add_meta_box(
3 'my_meta_box',
4 'My Custom Meta Box',
5 'display_my_meta_box',
6 'post',
7 'normal',
8 'high'
9 );
10 }
11 add_action( 'add_meta_boxes', 'my_custom_meta_box' );This creates a new box titled 'My Custom Meta Box' inside the normal section of the post editor, appearing near the top of the page.
Step 3: Add Content to the Meta Box
Next, define what should appear inside your custom box. The callback function passed to `add_meta_box()` handles that. Let’s create it:
function display_my_meta_box( $post ) {
$custom_text = get_post_meta( $post->ID, 'custom_text', true );
echo '<label for="custom_text">Custom Text:</label>';
echo '<input type="text" id="custom_text" name="custom_text" value="' . esc_attr( $custom_text ) . '" style="width:100%;" />';
}This function pulls any previously saved value from the database and displays it in a text input field. You can customize this with dropdowns, checkboxes, or even WYSIWYG editors depending on your needs.
Step 4: Save the Meta Box Data
Now that users can add data to your meta box, you need to make sure that data is saved when the post is updated. Here’s how to do it safely:
1function save_my_meta_box_data( $post_id ) {
2 // Prevent autosaves and revisions from triggering this function
3 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
4 if ( $parent_id = wp_is_post_revision( $post_id ) ) $post_id = $parent_id;
5
6 // Check if the custom field was submitted
7 if ( isset( $_POST['custom_text'] ) ) {
8 update_post_meta( $post_id, 'custom_text', sanitize_text_field( $_POST['custom_text'] ) );
9 }
10 }
11 add_action( 'save_post', 'save_my_meta_box_data' );This function makes sure WordPress only saves data during a normal post save (not autosaves or revisions). Then it updates or creates the custom field in the database using `update_post_meta()`.
Step 5: Full Working Example
1// Add the meta box
2 function my_custom_meta_box() {
3 add_meta_box(
4 'my_meta_box',
5 'My Custom Meta Box',
6 'display_my_meta_box',
7 'post',
8 'normal',
9 'high'
10 );
11 }
12 add_action( 'add_meta_boxes', 'my_custom_meta_box' );
13
14 // Display the meta box
15 function display_my_meta_box( $post ) {
16 $custom_text = get_post_meta( $post->ID, 'custom_text', true );
17 echo '<label for="custom_text">Custom Text:</label>';
18 echo '<input type="text" id="custom_text" name="custom_text" value="' . esc_attr( $custom_text ) . '" style="width:100%;" />';
19 }
20
21 // Save meta box data
22 function save_my_meta_box_data( $post_id ) {
23 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
24 if ( $parent_id = wp_is_post_revision( $post_id ) ) $post_id = $parent_id;
25 if ( isset( $_POST['custom_text'] ) ) {
26 update_post_meta( $post_id, 'custom_text', sanitize_text_field( $_POST['custom_text'] ) );
27 }
28 }
29 add_action( 'save_post', 'save_my_meta_box_data' );You now have a working custom meta box that adds a text field to your post editor, saves user input, and retrieves it automatically. You can extend this further to handle multiple fields or custom post types.
Useful Links
WordPress Developer Handbook: add_meta_box reference:
Guide to working with post meta in WordPress:
That’s all there is to it — a clean, reusable way to attach extra data to your posts without any plugins. Once you’re comfortable with this setup, you can expand it with multiple fields, dropdowns, or even repeatable groups for more advanced customizations.

I’m an experienced SEO professional with over a decade of helping over 100 businesses rank higher online, especially local businesses, e-commerce stores and SaaS. As the co-founder of LPagery, I specialize in practical, proven strategies for regular SEO and Local SEO success.