Skip to content
GPLWP Guides

How to create custom fields in WordPress

Developer Tools Time About 30 minutes 6 steps Updated 4 Sep 2026

DEVELOPER TOOLS
The short answer

WordPress stores custom fields as post metadata. For a one-off value, enable Custom Fields in the block editor and add a key and value. For a proper editor interface, create a custom meta box in code or use a field-builder plugin, then sanitise saved data and escape it when displayed.

The route
  1. 01Choose the field structure
  2. 02Add a field from the editor
  3. 03Create a custom meta box
  4. 04Save the submitted value safely
  5. 05Register metadata for integrations
  6. 06Display and maintain the value

What you need

  • A WordPress site with administrator access
  • A staging site or recent backup
  • A code editor and child theme or custom plugin if using code

Choose the field structure

Decide what information you need to store, where it belongs, and whether it is a single value or a collection. Examples include an event date, product reference, subtitle, rating, or external URL.

Use a clear, lowercase meta key such as event_date. Do not change the key later without migrating existing values, or the old data will appear to have disappeared.

Add a field from the editor

For a simple field, open a post or page in the block editor. Select the three-dot Options menu, choose Preferences, open General, enable Custom fields under Advanced, and reload the editor. The Custom Fields panel then appears below the content area.

Enter a key, enter its value, and select Add Custom Field. This is fine for occasional metadata, but it gives editors a plain key/value interface and does not provide field validation, conditional display, repeaters, or a polished editing experience.

Create a custom meta box

For a reusable editor field, add a meta box from a custom plugin or child theme. Hook a function to add_meta_boxes, call add_meta_box(), and render the input in its callback. The meta box is placed inside the normal post editing form, so it does not need its own submit button.

Limit the box to the post types that need it. Give the input a unique name, load its existing value with get_post_meta(), and use an appropriate HTML input type such as text, date, number, checkbox, or select.

Save the submitted value safely

Save the field from a save callback attached to save_post. Add a nonce with wp_nonce_field() when rendering the box, verify it with wp_verify_nonce(), check the user capability, and return during autosaves or revisions. The save_post action can run more than once for one update, so these checks matter.

Read only the expected value from $_POST. Unslash it, validate the allowed format, sanitise it with the function that matches its type, and save it with update_post_meta(). That function adds the key if it does not already exist. Do not trust raw form data, and do not process the entire request array.

Register metadata for integrations

If JavaScript, the block editor, or the REST API needs the value, register the key with register_post_meta() or register_meta(). Set the correct type, set single according to whether one or several values are returned, and add a sanitisation callback where necessary.

Set show_in_rest only when the value should be available through the REST API. The post type must support custom-fields, and array values need an appropriate REST schema. Exposing a field without considering permissions can make private data readable or writable, so use an authorisation callback for restricted values.

Display and maintain the value

Retrieve a single value with get_post_meta( $post_id, 'event_date', true ). Escape it at the point of output: use esc_html() for visible text, esc_attr() for an HTML attribute, esc_url() for a URL, and an appropriate escaping method for other contexts. Sanitising on save does not replace escaping on output.

For more than basic post metadata, Meta Box AIO is a practical shortcut. It provides a visual field and meta-box builder with multiple field types, conditional logic, custom post type and taxonomy support, and integrations. Use it when hand-built boxes would create too much maintenance, but keep the same rules for field naming, access control, validation, and output.

The fast route

Let Meta Box AIO do it

All-in-one custom-field builder with conditional logic and integrations, chosen when you need more than basic post metadata.

Get Meta Box AIO

Sources

  1. wordpress.org /documentation/article/assign-custom-fields/?utm_source=open…
  2. developer.wordpress.org /plugins/metadata/custom-meta-boxes/?utm_source=openai
  3. developer.wordpress.org /plugins/security/securing-input/?utm_source=openai
  4. developer.wordpress.org /reference/functions/register_meta/?utm_source=openai
  5. developer.wordpress.org /reference/functions/get_post_meta/?utm_source=openai
  6. developer.wordpress.org /plugins/metadata/?utm_source=openai

Questions

Are custom fields built into WordPress?
Yes. WordPress supports custom fields as key/value post metadata, and you can add them from the block editor after enabling the Custom fields panel in Preferences. The built-in interface is suitable for occasional values, but it does not create a tailored form or enforce strong validation. A custom meta box or field-builder plugin is better for fields used regularly by editors.
Where are WordPress custom fields stored?
WordPress stores post custom fields in the post metadata system, commonly called post meta. Each entry has a key and value associated with a post, page, or custom post type. Use functions such as <code>get_post_meta()</code>, <code>add_post_meta()</code>, and <code>update_post_meta()</code> rather than writing directly to the database.
Why does my custom field not appear in the editor?
The Custom Fields panel is hidden by default in the block editor until you enable it from Options, Preferences, General, and Advanced. A custom meta box can also be hidden by screen preferences, restricted to another post type, or marked as incompatible with the block editor. Check the selected screen, user permissions, and meta-box compatibility settings before changing code.
Why is my custom field value not saving?
A custom field usually fails to save because the save callback is not running, the input name does not match the submitted key, the nonce or capability check fails, or the code returns during an autosave or revision. Also check that your code reads the expected <code>$_POST</code> value and calls <code>update_post_meta()</code> with the correct post ID and meta key.
How do I show a custom field on the front end?
Retrieve the value with <code>get_post_meta()</code> in the relevant template or block rendering code, then escape it for the output context. Use <code>esc_html()</code> for text, <code>esc_attr()</code> for attributes, and <code>esc_url()</code> for links or image URLs. A saved value should never be echoed directly, even if it came from an administrator.