Adding Metaboxes - rogerlos/cmb2-metatabs-options GitHub Wiki
You can add metaboxes to a CMO options page by:
- Adding boxes to an array which you inject into the class via the
boxes
argument, see the "Quick Example". This array can contain either/mixed CMB2 metabox objects or CMB2 metabox id strings. - Adding boxes directly to the class (ie, "hard coding" them), doing so within the
cmb2_metaboxes()
method - Adding boxes elsewhere in your program in the usual CMB2 fashion, as long as you do so before calling
new Cmb2_Metatabs_Options()
.
If you use the final option, you still need to heed the following "rules":
Important: There are two parameters you must add to your metaboxes for them to display and save successfully on your options page.
Consider this simple box:
$cmb = new_cmb2_box(
array(
'id' => 'test_metabox',
'title' => __( 'Test Metabox', 'cmb2' ),
'show_on' => array(
'key' => 'options-page',
'value' => array( 'your_option_name', ),
),
)
);
$cmb->object_type( 'options-page' );
Note the show_on
argument and its value, an array:
'show_on' => array(
'key' => 'options-page',
'value' => array( 'your_option_name', ),
)
The first argument, key
, must be exactly as shown, "options-page"; this is the trigger CMB2 uses to save "settings" instead of "metadata".
The second argument, value
, is a plain array containing the options page id (or multiple ids, if you want to share the box on multiple options pages--you cannot, unfortunately, share metaboxes between "post editors" and "options pages" because of this internal CMB2 distinction between options and metadata).
The last line in our sample:
$cmb->object_type( 'options-page' );
Explicitly sets the object-type for this box. If you skip this, the initial load of the options page will not show your saved values--your metaboxes will display, but saved values will not be shown in your fields. (I believe this is a minor bug in CMB2, but it's easily worked around.)