2026年11月12日

2026年11月12日

Create Custom Fields with Meta Box: Register with rwmb_meta_boxes

This article is a localized edition of the Japanese original. Read the Japanese original

Meta Box is a plugin that adds your own input fields (custom fields) to the edit screen of posts, pages and custom post types. You define fields as a PHP array and register them with the rwmb_meta_boxes filter. This article walks through registering and displaying fields, using the example of adding event details (venue, capacity, format and registration URL) to posts.

What Meta Box does

  • Adds input fields to the edit screen of posts, pages and custom post types
  • Provides many field types, including text, number, URL, select, date and image
  • Saves the values as post metadata (the wp_postmeta table) so your theme templates can display them

The core Meta Box plugin distributed on wordpress.org is a framework for defining fields in code. The screen for creating fields in the admin (MB Builder) is an extension; as of October 2026, a free package called "Meta Box Lite" that includes MB Builder is distributed on the official site (metabox.io). This article covers registering fields in code, which works with the core plugin alone.

Step 1: Install Meta Box

In the admin, go to Plugins → Add New Plugin, search for "Meta Box", then install and activate the plugin whose author is "Meta Box". If you can use WP-CLI, this command does the same.

wp plugin install meta-box --activate

Step 2: Register the fields

Fields are registered with the rwmb_meta_boxes filter. The filter receives the array of meta boxes registered so far; add your definition and return it. Put the code in your child theme's functions.php or in your own plugin (code in a parent theme is lost when the theme updates).

<?php
// Add to your child theme's functions.php or to your own plugin
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
    $meta_boxes[] = [
        'title'      => 'Event details',
        'id'         => 'event_info',
        'post_types' => [ 'post' ],
        'context'    => 'normal',
        'priority'   => 'high',
        'fields'     => [
            [
                'name' => 'Venue',
                'id'   => 'event_venue',
                'type' => 'text',
                'desc' => 'Example: Main Hall, City Civic Center',
            ],
            [
                'name' => 'Capacity',
                'id'   => 'event_capacity',
                'type' => 'number',
                'min'  => 0,
            ],
            [
                'name'        => 'Format',
                'id'          => 'event_format',
                'type'        => 'select',
                'placeholder' => 'Select one',
                'options'     => [
                    'onsite' => 'On-site',
                    'online' => 'Online',
                    'hybrid' => 'Hybrid',
                ],
            ],
            [
                'name' => 'Registration page URL',
                'id'   => 'event_url',
                'type' => 'url',
            ],
        ],
    ];

    return $meta_boxes;
} );

The post edit screen now shows the "Event details" fields. Registration alone also takes care of saving, so you do not need to write add_meta_box() or save_post code yourself.

Main meta box settings

Key Description
title Heading shown on the edit screen (required)
id Identifier of the meta box. Generated from the title if omitted, but setting it explicitly is recommended
post_types Post types to show it on, as a string or array (defaults to post)
context Position, such as normal (default) or side. The block editor supports only normal and side
priority high (default) or low
autosave Whether to include the fields in autosave (default false)
fields Array of field definitions

Main field settings

Key Description
name Label of the input field
id Identifier of the field, used as is as the post meta key
type Field type (text, number, url, select, etc.)
desc Description shown below the input field

Because the field id becomes the meta key, add a prefix such as event_ so it does not collide with other plugins or themes.

Step 3: Display the values in a template

Read saved values with rwmb_meta(). The first argument is the field id; if you omit the third argument, the current post is used. The following example is for use inside the loop of single.php or a similar template.

<?php
// Inside the loop of single.php etc. (outputs nothing if Meta Box is inactive)
if ( function_exists( 'rwmb_meta' ) ) :
    $venue    = rwmb_meta( 'event_venue' );
    $capacity = rwmb_meta( 'event_capacity' );
    $format   = rwmb_meta( 'event_format' );
    $url      = rwmb_meta( 'event_url' );
    ?>
    <dl class="event-info">
        <?php if ( $venue ) : ?>
            <dt>Venue</dt>
            <dd><?php echo esc_html( $venue ); ?></dd>
        <?php endif; ?>

        <?php if ( '' !== (string) $capacity ) : ?>
            <dt>Capacity</dt>
            <dd><?php echo esc_html( number_format( (int) $capacity ) ); ?> people</dd>
        <?php endif; ?>

        <?php if ( $format ) : ?>
            <dt>Format</dt>
            <dd><?php rwmb_the_value( 'event_format' ); ?></dd>
        <?php endif; ?>

        <?php if ( $url ) : ?>
            <dt>Registration</dt>
            <dd><a href="<?php echo esc_url( $url ); ?>">Open the registration page</a></dd>
        <?php endif; ?>
    </dl>
<?php endif; ?>

Three points to note:

  • For a select field, rwmb_meta() returns the stored value (such as online). To display the label ("Online"), use rwmb_the_value(), which outputs the value directly by default
  • Escape output with esc_html() and esc_url()
  • Wrapping the code in function_exists( 'rwmb_meta' ) prevents a fatal error in the template if Meta Box is deactivated

Reading values without depending on the plugin

Single values such as text and numbers are stored in wp_postmeta with the field id as the meta key, so WordPress's standard get_post_meta() can read them too.

<?php
// Reading without depending on the plugin (for single-value fields)
$venue = get_post_meta( get_the_ID(), 'event_venue', true );
if ( $venue ) {
    echo '<p>Venue: ' . esc_html( $venue ) . '</p>';
}

However, fields with multiple selection or cloning enabled are stored differently, so reading them with rwmb_meta() is more reliable.

Troubleshooting

  • The fields do not appear: check the edit screen's display settings (in the block editor, Preferences, opened from the "⋮" menu at the top right) to make sure the meta box is not hidden. Also check that post_types includes the post type
  • The position does not change in the block editor: even if you set context to something like after_title, the block editor shows meta boxes only as normal (below the content) or side (in the sidebar)
  • Values are not displayed: check that the id passed to rwmb_meta() in the template matches the id of the registered field

Cautions

  • If you change a field id later, saved values stay under the old meta key and can no longer be read. Decide on names before you start using the fields
  • A large number of fields makes the edit screen slower and data entry more tedious. Keep only the fields your templates actually use
  • Before trying this on a live site, check display and saving on a staging or local environment

Summary

  • The core Meta Box plugin is a framework for defining fields in code. Add an array in the rwmb_meta_boxes filter and return it, and you get the input fields and saving
  • Read values with rwmb_meta() and display select labels with rwmb_the_value(). Escape output
  • Single-value fields can also be read with get_post_meta(). Prefix the id and do not change it once the fields are in use

Related articles

Questions about WordPress? The publisher of this site is based in Japan and replies in Japanese. Contact (Japanese)

お気軽にご相談ください

お見積りへ お問い合わせへ
▲