Skip to main content

Creating fields with code

Use code to create custom fields if you want to keep everything in your themes or plugins. This lets you reuse them across websites and put them under version control like Git.

Registering custom fields with PHP

To create custom fields, use the rwmb_meta_boxes filter to register field groups. This filter accepts one parameter - the array of field groups:

add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
$meta_boxes[] = [
'title' => 'Event details',
'post_types' => 'event',
'fields' => [
[
'name' => 'Date and time',
'id' => 'datetime',
'type' => 'datetime',
],
[
'name' => 'Location',
'id' => 'location',
'type' => 'text',
],
[
'name' => 'Map',
'id' => 'map',
'type' => 'osm',
'address_field' => 'location',
],
],
];

// Add more field groups if you want
// $meta_boxes[] = ...

return $meta_boxes;
} );

Each field group has settings and a list of fields, covered below.

Field group settings

Each field group has settings for location or appearance:

NameDescription
idID, must be unique. Optional. If it is absent, it will be generated from the title. We recommend specifying the ID explicitly to avoid issues when the title contains non-ASCII characters (e.g. Chinese, Japanese, Korean, or Vietnamese).
titleThe field group title. Required.
post_typesCustom post types for the field group. Can be a string or an array of slugs. Must be in lowercase (like the slug). Optional. Default: post.
contextWhere the field group is displayed. See below for a list of field group contexts. Optional.
styleKeep the default WordPress field group style (default) or remove the wrapper box and display fields without styling (seamless).
closedWhether to collapse the field group when the page loads? Optional. Default: false.
priorityPriority within the context where the box is displayed (high or low). Optional. Default: high.
default_hiddenHide the field group by default (true or false)? Toggle this with the checkbox in screen Help (top right). Optional. Default false.
autosaveAuto save the custom fields' values (like post content and title)? Optional. Default: false.
media_modalAdd custom fields to media modal when viewing/editing an attachment. Works only when post_types is or contains attachment. Optional. Default false.
classCustom CSS class for the field group wrapper. Optional.
Media modal limitation

Only simple fields such as text, select, radio, and checkbox work in the media modal. Fields that require custom JavaScript do not work.

Contexts

The plugin supports the following contexts (locations) where a field group can appear:

NameDescription
normalBelow the post editor. This is the default value.
advancedBelow the normal section.
sideOn the right sidebar.
form_topTop of the post form, before the post title
after_titleAfter post title
after_editorAfter the post content editor, but before normal section
before_permalinkBefore permalink
Block editor

The block editor editor supports only normal and side contexts. Other contexts do not work.

Fields

Add fields to a field group via the fields key. Each field is an array of settings.

Meta Box supports more than 40 field types. They share some common settings but also offer unique settings per field type.

Field types

Know the field type and how it works before adding a field. This helps you choose the right type for your data.

The list below shows all supported field types in alphabetical order with a brief description. The field type key is used for code reference. See the Field types menu for details on how they look and how to use them.

These basic field types do not require an extra library. They use the WordPress UI.

TypeKeyDescription
CheckboxcheckboxA simple checkbox, usually used for Yes/No question
Checkbox listcheckbox_listA list of checkboxes where you can select multiple choices
RadioradioRadio input where you can select only one choice
SelectselectSelect dropdown where you can select one or multiple choice
TexttextA single-line text input
TextareatextareaA paragraph text input

Field settings

Each field has settings for where and how data is loaded and saved. All fields share common settings. Each field type also has unique settings. Extension settings are explained in each extension docs.

Below is the list of settings with a brief description. The keys are for reference in code.

general settings

NameKeyDescription
LabelnameField label. Optional. If empty, the field input is 100% width.
IDidField ID. Required and must be unique. The ID is used as meta_key when saving to the database. Use only numbers, letters, and underscores (and rarely dashes).
TypetypeField type. Required.
Label descriptionlabel_descriptionLabel description, displayed below the field label. Optional.
Input descriptiondescField description, displayed below the field input. Optional.
Default valuestdDefault value. Optional.
PlaceholderplaceholderPlaceholder text for the input or select box. Optional.
RequiredrequiredWhether the field is required (true or false). Optional. Default false.
DisableddisabledWhether the field is disabled (true or false). Optional. Default false.
Read onlyreadonlyWhether the field is read only (true or false). Optional. Default false.
MultiplemultipleDoes the field have multiple values (like the select field)? Optional. Default false.
CloneablecloneIs the field clonable (repeatable)? true or false. Optional. Default false.
Sortablesort_cloneAbility to drag-and-drop reorder clones (true or false). Optional. Default false.
Clone default valueclone_defaultClone the default value of fields? true or false (default).
Clone as multipleclone_as_multipleWhether to store clone values in multiple rows in the database? Optional. Default false.
Max number of clonesmax_cloneMaximum number of clones. Optional. Default 0 (unlimited).
Min number of clonesmin_cloneMinimum number of clones. Optional. Default 0.
Add more textadd_buttonThe text for Add more clone button. Optional. Default "+ Add more".
Field ID prefix

You can add a prefix to field IDs to prevent using the same ID with other scripts. If you want to hide the fields in the default WordPress Custom Fields meta box, use underscore (_) as the prefix.

Field-specific settings

In addition to common settings, each field type has its own settings. See the left menu for details on each field type.

Code examples

To save time, we have prepared code examples in the Meta Box Code Snippet Library.

Shorthand syntax

For text field type, you can omit the type settings, like this:

add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
$meta_boxes[] = [
'title' => 'Event details',
'post_types' => 'event',
'fields' => [
[
'name' => 'Date and time',
'id' => 'datetime',
'type' => 'datetime',
],
[
'name' => 'Location',
'id' => 'location',
],
[
'name' => 'Map',
'id' => 'map',
'type' => 'osm',
'address_field' => 'location',
],
],
];

return $meta_boxes;
} );

And if the id of the field can be auto-generated from the field name, you can omit it and define the field as a simple string:

add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
$meta_boxes[] = [
'title' => 'Event details',
'post_types' => 'event',
'fields' => [
[
'name' => 'Date and time',
'id' => 'datetime',
'type' => 'datetime',
],
'Location',
[
'name' => 'Map',
'id' => 'map',
'type' => 'osm',
'address_field' => 'location',
],
],
];

return $meta_boxes;
} );

Video tutorial

This video shows all field types and field settings:

FAQ

Why does not my default value work?

The std value works only if the field group has not been saved before. This applies to all fields in the group, not just the field with std. If any field has a value, std does not work for other fields, even new ones you add.

Examples:

When you create a new post, no fields have values. std works for all fields.

When you edit an existing post with a field group, some fields may have values. Then std does not work for all fields. If you add a new field to the field group, std does not work for that field, even if it has no value. The field group was saved before.

# Link to this question
Why does not my context work?

The context may not work if you dragged and dropped field groups to reorder them. WordPress saves the position and uses it instead of the context value. The order is saved in user meta meta-box-order_{screen id}:

meta box order

To fix this, delete the user meta from the database. The context will work again.

# Link to this question