How to Add Custom Fields in WordPress

WordPress has long been a preferred platform for building websites, blogs, and even e-commerce applications, especially when paired with WooCommerce and other powerful plugins. One of the reasons WordPress remains so popular is its extensive customization options, making it suitable for various types of sites and applications.
While customizing WordPress websites has become easier due to the availability of many plugins, there are situations where you might need additional functionality—like creating custom fields for posts, pages, or other content types—that aren’t available by default. In such cases, creating Custom Fields can help extend WordPress’s functionality without much hassle.
In this post, we’ll walk through two different methods for adding custom fields in WordPress:
- Using a plugin like Advanced Custom Fields (ACF)
- Manually creating custom fields through code
Create Custom Fields Using a Plugin
For most developers, using a plugin to add custom fields is the easiest and most efficient approach. There are several plugins available in the WordPress plugin repository, but one of the most popular and feature-rich options is Advanced Custom Fields (ACF) by Elliot Condon.

ACF has 30+ field types available to choose and use

Steps to Create Custom Fields Using ACF Plugin
- Install the Plugin
Go to the Plugins section in your WordPress admin dashboard, search for Advanced Custom Fields, and install the plugin. After activating it, you’ll notice a new menu item called Custom Fields in the sidebar. - Create a New Field Group
In the Custom Fields section, click on Add New to create a new field group. A field group allows you to bundle multiple custom fields together for a specific post type (e.g., posts, pages, or custom post types). - Add Custom Fields
Add the necessary fields to your group, such as text inputs, checkboxes, or even date pickers. For each field, you can define a label, name, and other settings. You can also specify a location where these fields will appear (e.g., on posts, pages, or custom post types). - Configure Conditional Logic (Optional)
ACF allows you to apply conditional logic, so you can display fields only when certain criteria are met (e.g., display a field only if a specific category is selected). - Save and Display the Fields
After saving your field group, the custom fields will be available on the relevant pages or posts. You can then display the custom field values in your theme templates by using ACF functions likeget_field()
.

ACF also provides support to create custom post types, making it a one-size-fits-all solution for custom fields and post types. If you are looking to create Custom Post Types manually, read here – How to create a Custom Post Type in WordPress
Manually Adding Custom Fields via Code
If you prefer more control or want to avoid using plugins, you can manually create custom fields by adding code to your theme’s functions.php
file. This method involves using WordPress’s built-in meta box system to add custom fields to the WordPress admin.
Steps to Create Custom Fields via Code
- Register a Custom Meta Box
The first step is to create a custom meta box where you’ll add the custom fields. You can use theadd_meta_box()
function to register the meta box:
function vi_meta_box() {
add_meta_box(
'custom_meta_box_id',
esc_html__('Custom Field', 'your_theme'),
'custom_meta_box_content',
'post', // Post type where you want to show the custom field
'advanced',
'high'
);
}
add_action('add_meta_boxes', 'vi_meta_box');
- Create the Field’s Content
Next, define the content of the custom field. This will be a simple text input field, but you can modify this to suit your needs:
function vi_meta_box_content($post) {
// Retrieve any existing value for the custom field
$custom_value = get_post_meta($post->ID, '_custom_field', true);
// Output the custom field's HTML
echo '<label for="custom_field">Custom Field: </label>';
echo '<input type="text" name="custom_field" id="custom_field" value="' . esc_attr($custom_value) . '" style="width: 100%"/>';
wp_nonce_field('vi_meta_box_nonce', 'vi_meta_box_nonce');
}
- Save the Custom Field Data
Now that you’ve added the input field, you need to ensure the data is saved when the post is updated. You can do this with thesave_post
hook:
function save_vi_meta_data($post_id) {
// Verify nonce
if (!isset($_POST['vi_meta_box_nonce']) || !wp_verify_nonce($_POST['vi_meta_box_nonce'], 'vi_meta_box_nonce')) {
return $post_id;
}
// Check if the user has permission to edit the post
if (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// Sanitize the input and update the meta field
if (isset($_POST['custom_field'])) {
$custom_value = sanitize_text_field($_POST['custom_field']);
update_post_meta($post_id, '_custom_field', $custom_value);
}
}
add_action('save_post', 'save_vi_meta_data');
- Display the Custom Field in Templates
To display the custom field value on the front end, you can useget_post_meta()
within your theme template files:
$custom_value = get_post_meta($post->ID, '_custom_field', true);
echo esc_html($custom_value);
Conclusion: Plugin or Code?
Both methods—using a plugin like ACF or adding custom fields manually—have their advantages. Plugins like Advanced Custom Fields are easier to use and more flexible, especially when working on larger projects, as they allow you to manage fields directly from the WordPress admin panel. On the other hand, adding custom fields manually gives you full control over the process and eliminates the need for additional plugins.
For smaller projects or when you prefer lightweight solutions, creating custom fields via code may be the best route. However, for more complex websites or those requiring many custom fields, a plugin will save you time and effort.
Feel free to leave a comment if you have any questions or if you’d like further clarification on either method!
Image Credit : Photo by Szabó Viktor on Pexels.com
1 thought on “How to Add Custom Fields in WordPress”