Guide

Post Meta Fields Ability

Ability for creating or updating the custom field configuration attached to a post type.


native-custom-fields/save-post-meta-fields

Creates or updates the meta box and field configuration for a post type. If a configuration already exists for the given post_type, it is overwritten.


Input Schema

{
  "type": "object",
  "required": ["post_type", "sections"]
}

Top-level Properties

PropertyRequiredTypeDescription
post_typeYesstringSlug of the post type to attach fields to
sectionsYesarrayArray of meta box definitions

Section (Meta Box) Schema

{
  "type": "object",
  "required": ["meta_box_id", "meta_box_title"]
}
PropertyRequiredTypeDefaultDescription
meta_box_idYesstringUnique slug for the meta box
meta_box_titleYesstringMeta box title displayed in the admin screen
meta_box_contextNostring"advanced"Where the meta box appears: normal, side, advanced
meta_box_priorityNostring"default"Meta box priority: default, high, core, low
fieldsNoarray[]Fields inside this meta box (see field-schema.md)

meta_box_context Values

ValueDescription
normalFull-width, below the content editor
sideIn the right sidebar
advancedBelow the normal boxes (default)

meta_box_priority Values

ValueDescription
defaultStandard ordering (default)
highAt the top of the context
coreSorted with core meta boxes
lowAt the bottom of the context

Output Schema

{
  "status": true,
  "message": "..."
}
FieldTypeDescription
statusbooleantrue on success, false on failure
messagestringResult description or error message

Internal Behaviour

  • post_type is sanitized with sanitize_key().
  • meta_box_id is sanitized with sanitize_key().
  • meta_box_title is sanitized with sanitize_text_field().
  • Builder slug format: native_custom_fields_post_meta_fields_builder_{post_type}
  • On success, the same values are also stored via OptionService::saveOptions().
  • Field values are stored at runtime in the wp_postmeta table using the field name as the meta key.

Example

{
  "post_type": "book",
  "sections": [
    {
      "meta_box_id": "book_details",
      "meta_box_title": "Book Details",
      "meta_box_context": "normal",
      "meta_box_priority": "high",
      "fields": [
        {
          "fieldType": "text",
          "name": "author",
          "fieldLabel": "Author",
          "required": true,
          "field_custom_info": {
            "placeholder": "Enter author name"
          }
        },
        {
          "fieldType": "number",
          "name": "page_count",
          "fieldLabel": "Page Count",
          "field_custom_info": {
            "min": 1,
            "max": 9999,
            "step": 1
          }
        },
        {
          "fieldType": "select",
          "name": "language",
          "fieldLabel": "Language",
          "default": "en",
          "field_custom_info": {
            "options": "English:en, German:de, French:fr"
          }
        }
      ]
    },
    {
      "meta_box_id": "book_media",
      "meta_box_title": "Media",
      "meta_box_context": "side",
      "fields": [
        {
          "fieldType": "media_library",
          "name": "cover_image",
          "fieldLabel": "Cover Image"
        }
      ]
    }
  ]
}

Success Response

{
  "status": true,
  "message": "Post meta fields saved successfully."
}

Error Responses

{ "status": false, "message": "post_type is required." }
{ "status": false, "message": "sections is required." }

Permission

Requires the manage_options WordPress capability.


Was this helpful?