Options¶
Register custom metaboxes on DMS admin edit screens.
DMS uses CMB2 under the hood, but you register through dms_register_metabox() rather than CMB2 directly - DMS collects your definition and builds the CMB2 box at the right moment in its own admin flow.
dms_register_metabox( array $metabox_data ): void¶
Registers a custom metabox. DMS stores the definition and renders it later on cmb2_admin_init.
When to register¶
Call it on the dms_init action (or any point before cmb2_admin_init fires). DMS renders every registered metabox on cmb2_admin_init, so anything registered after that never appears.
dms_init runs during plugin load, well before cmb2_admin_init, and is the same hook core DMS uses to register its own metaboxes - so it's the safe, recommended place.
Registering directly at file-load (outside any hook) is too early:
dms_options()/ the CMB2 layer may not be ready. Registering onadmin_initor later is too late.
Parameters¶
| Name | Type | Description |
|---|---|---|
$metabox_data |
array |
Metabox definition. See structure below. |
Structure¶
The array is not flat - the box settings live under a data key, and fields under a separate fields key:
| Key | Type | Description |
|---|---|---|
data |
array |
The CMB2 box config passed to new_cmb2_box(). Requires id; typically also title, object_types, context, priority. |
fields |
array |
List of CMB2 field definitions, each with at least id, type, name. |
tabs |
array |
(optional) Tabbed layout config, when you want the fields grouped into tabs. |
Returns¶
void
Example¶
add_action( 'dms_init', function () {
dms_register_metabox( [
'data' => [
'id' => 'my_plugin_listing_meta',
'title' => 'My Plugin Data',
'object_types' => dms_get_types( true ), // all DMS inventory post types
'context' => 'normal',
'priority' => 'high',
],
'fields' => [
[
'id' => 'my_custom_field',
'type' => 'text',
'name' => 'Custom Field',
],
],
] );
} );
Read the saved value back off a listing with dms_get_listing( $id )->get_meta( 'my_custom_field' ).
Notes¶
- Each box is filterable at render time via
dms_metabox_{id}_data, and each field viadms_metabox_{id}_field_{field_id}- use these to tweak a box you (or core) registered. - Metaboxes only render on the relevant admin edit screens; they add nothing on the front end.