How to add custom fields in WordPress
You can add a simple custom field from the WordPress editor without a plugin. For a proper field used by a theme, custom post type or block, register post meta in PHP, then save and display it with the relevant WordPress functions. A plugin is quicker when you need a polished field UI.
- 01Choose the field structure
- 02Add a field from the editor
- 03Register the field in PHP
- 04Build a tailored input box
- 05Save and validate the value
- 06Read the field on the site
What you need
- WordPress admin access
- A clear field name and value format
- A child theme or small custom plugin for PHP changes
Choose the field structure
Decide whether the value belongs to one post, page or custom post type. Examples include a subtitle, external URL, event date or product reference. Give the field a stable key such as event_date; do not change the key later unless you also migrate existing values.
Use a single text value where possible. WordPress stores arrays and objects as serialised data, which makes them harder to query and edit safely. Keys beginning with an underscore are treated as protected and are hidden from the standard Custom Fields list.
Add a field from the editor
For a one-off or simple text value, edit the post or page and open the Custom Fields panel. In the block editor, select the three-dot Options menu, choose Preferences, enable Custom fields under Advanced, then reload the editor. Enter the key in Name, enter the value, and save the post.
The panel is fine for occasional values, but it has no tailored labels, validation or helpful input controls. It is also easy for an editor to mistype a key, creating a second field that your template will never read.
Register the field in PHP
For a field used repeatedly, register it with register_post_meta() on the init hook. Set the post type, key, single to true, and the correct type, such as string, integer or boolean. Add show_in_rest when the block editor or REST API must read and write the value.
The post type must support custom-fields for registered post meta to work correctly. If you control a custom post type, include that support when registering it.
Build a tailored input box
If the default panel is too awkward, create an admin meta box with add_meta_box() and attach it to the add_meta_boxes hook. Render a labelled input, select or textarea, and load the existing value with get_post_meta(). The meta box can target posts, pages or a named custom post type.
Put this code in a small site plugin or a child theme rather than editing a parent theme. The manual route gives you full control, but you must maintain the PHP, field markup, permissions and compatibility with the block editor yourself.
Save and validate the value
Save the submitted value on save_post, usually with update_post_meta(). Before saving, verify a nonce, check that the current user can edit the post, ignore autosaves and revisions, confirm the expected field exists, and sanitise it for its type. Use text sanitisation for plain text, URL sanitisation for URLs, and appropriate validation for dates or numbers.
A common Friday-afternoon failure is saving every save_post request without these checks. Autosaves, revisions and unrelated updates can overwrite a good value or save empty data.
Read the field on the site
Retrieve the value with get_post_meta( get_the_ID(), 'your_key', true ), then escape it for its output context. Use HTML escaping for text, attribute escaping inside an HTML attribute, and URL escaping for links. Do not print stored values directly.
If you need the value in a block or through the REST API, register it with show_in_rest. For block bindings, the field must also be registered for REST access and its key must not start with an underscore.
Let png-php-field25 do it
Appears related to PHP-defined fields, but the listing provides too little detail to compare it reliably with custom-field tools.
Sources
- developer.wordpress.org /reference/functions/add_post_meta/?utm_source=openai
- wordpress.org /documentation/article/assign-custom-fields/?utm_source=open…
- developer.wordpress.org /reference/functions/register_post_meta/?utm_source=openai
- developer.wordpress.org /reference/functions/add_meta_box/?utm_source=openai
- developer.wordpress.org /block-editor/how-to-guides/metabox/?utm_source=openai
Questions
- Can I add custom fields without a plugin?
- Yes, WordPress includes a built-in Custom Fields panel for simple post metadata. Enable it from the editor’s Options and Preferences area, add a name and value, then save the post. For a controlled field with validation or a custom input, you still need PHP in a site plugin or child theme, or a field-management plugin.
- Why is the Custom Fields panel missing?
- The panel is hidden by default in the block editor on some sites. Open the three-dot Options menu, choose Preferences, enable Custom fields under Advanced, and reload the editor after saving the post if prompted. On classic editing screens, check Screen Options and enable the Custom Fields module.
- What is the difference between custom fields and post meta?
- Custom fields are the WordPress admin name for post metadata: key-and-value data attached to a post, page or custom post type. The database stores it as post meta, and functions such as <code>get_post_meta()</code> and <code>update_post_meta()</code> read and write it. A custom field plugin usually adds a better editing interface around the same underlying data.
- Should I use a plugin for custom fields?
- Use the built-in panel for a few simple values, and use PHP when you need a small, site-specific field with full control. A plugin is sensible when non-developers need repeatable fields, date pickers, media selectors, validation or a maintained admin interface. The listed product png-php-field25 appears related to PHP-defined fields, but its listing gives too little detail to compare it reliably with established custom-field tools.