Ask any question about WordPress here... and get an instant response.
Post this Question & Answer:
How can I add custom fields to a WordPress post type without a plugin?
Asked on Jan 10, 2026
Answer
To add custom fields to a WordPress post type without using a plugin, you can utilize the `add_meta_box` function in your theme's `functions.php` file. This allows you to create custom fields that appear in the post editor screen for your specified post type.
<!-- BEGIN COPY / PASTE -->
function my_custom_meta_box() {
add_meta_box(
'my_meta_box_id', // Unique ID
'Custom Field Title', // Box title
'my_meta_box_callback', // Content callback, must be of type callable
'post' // Post type
);
}
add_action('add_meta_boxes', 'my_custom_meta_box');
function my_meta_box_callback($post) {
$value = get_post_meta($post->ID, '_my_meta_key', true);
echo '<label for="my_field">Description for this field</label>';
echo '<input type="text" id="my_field" name="my_field" value="' . esc_attr($value) . '" />';
}
function save_my_meta_box_data($post_id) {
if (array_key_exists('my_field', $_POST)) {
update_post_meta(
$post_id,
'_my_meta_key',
sanitize_text_field($_POST['my_field'])
);
}
}
add_action('save_post', 'save_my_meta_box_data');
<!-- END COPY / PASTE -->Additional Comment:
- Replace 'post' with your custom post type slug if needed.
- Ensure you use proper sanitization and validation for security.
- Custom fields can be displayed in the front-end using `get_post_meta()`.
- Consider using WordPress hooks to extend functionality as needed.
Recommended Links:
