Registering Meta Fields — Developer Guide — Pure Metafields

Docs/Developer Guide/Registering Meta Fields

Registering Meta Fields

Return an array of metabox definitions from the tp_meta_boxes filter. Each field definition takes id, type, label, plus type-specific options.

The tp_meta_boxes filter

PHP · functions.php
add_filter( 'tp_meta_boxes', function( $boxes ) {
    $boxes[] = [
        'metabox_id' => 'page_hero',
        'title'      => 'Page Hero',
        'post_type'  => [ 'page' ],
        'context'    => 'normal',
        'priority'   => 'high',
        'fields'     => [
            [ 'id' => 'hero_title',    'type' => 'text',  'label' => 'Title' ],
            [ 'id' => 'hero_image',    'type' => 'image', 'label' => 'Background' ],
            [ 'id' => 'hero_enabled',  'type' => 'switch', 'label' => 'Show hero?' ],
        ],
    ];
    return $boxes;
} );

Every field definition can also take description, default, placeholder, a conditional rule, and an optional sanitize_callback. Type-specific keys (like options for a select, or fields for a repeater) are covered per type in Available Field Types.

Bind a metabox to a post format

Add a post_format key and the metabox shows only for posts using that format — gallery, audio, video, quote, and so on. It appears and hides live as the editor switches the post's format.

PHP
$boxes[] = [
    'metabox_id'  => 'post_gallery_meta',
    'title'       => 'Gallery',
    'post_type'   => 'post',
    'post_format' => 'gallery',   // only on gallery-format posts
    'fields'      => [
        [ 'id' => 'gallery_images', 'type' => 'gallery', 'label' => 'Images' ],
    ],
];

User-profile fields

The tp_user_meta filter takes the identical field-array shape and adds fields to the user profile screen instead of a post. It's the code equivalent of building a metabox in the Meta Builder and choosing the profile fields option — see Integrations → User Meta for a full example and how to read the saved values back.

Next: grouping fields

A flat fields array works for a handful of fields. For repeatable groups or option-panel sections, continue to Creating Field Groups.