Examples — Integrations — Pure Metafields

Docs/Integrations/Examples

Examples

A few complete field definitions that combine what's covered elsewhere in this section. For longer, step-by-step walkthroughs, see Tutorials.

A portfolio item with a repeater gallery

Assumes a project custom post type already registered in Post Types.

PHP · functions.php
add_filter( 'tp_meta_boxes', function( $boxes ) {
    $boxes[] = [
        'metabox_id' => 'project_meta',
        'title'      => 'Project Details',
        'post_type'  => [ 'project' ],
        'fields'     => [
            [ 'id' => 'client_name', 'type' => 'text', 'label' => 'Client' ],
            [ 'id' => 'gallery', 'type' => 'repeater', 'label' => 'Gallery Items',
              'fields' => [
                  [ 'id' => 'image', 'type' => 'image', 'label' => 'Image' ],
                  [ 'id' => 'caption', 'type' => 'text', 'label' => 'Caption' ],
              ] ],
        ],
    ];
    return $boxes;
} );
PHP · single-project.php
$rows = tpmeta_field( 'gallery' ) ?: [];
foreach ( $rows as $row ) {
    $img = tpmeta_image( $row['image'], 'large' );
    printf( '<figure><img src="%s"><figcaption>%s</figcaption></figure>',
        esc_url( $img['url'] ), esc_html( $row['caption'] ) );
}

A metabox bound to a post format

Only shows on posts using the gallery format:

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

A theme options section with an accent color that outputs CSS

PHP
TPMeta_Options::set_section( 'mytheme_options', [
    'id'     => 'branding',
    'title'  => 'Branding',
    'fields' => [
        [ 'id' => 'accent_color', 'type' => 'colorpicker', 'label' => 'Accent Color',
          'default' => '#3362FF',
          'output'  => [ [ 'selector' => 'a, .btn-primary', 'property' => 'color' ] ] ],
    ],
] );

With the panel's output_css set to true, this prints a, .btn-primary { color: #3362FF; } into <head> automatically — no template code needed.

Want the show/hide behavior too?

Adding a conditional rule to any field above works the same way in a metabox or an option section — see Advanced Topics → Dynamic Data.