CMB2
Overview
The CMB2 module is one of the largest parts of the library. It provides a fluent interface for creating CMB2 boxes, fields, groups, options pages, and field variations while also integrating with the library’s meta repository and REST helpers.
Primary types
Lipe\Lib\CMB2\BoxLipe\Lib\CMB2\FieldLipe\Lib\CMB2\GroupLipe\Lib\CMB2\Options_PageLipe\Lib\CMB2\Term_BoxLipe\Lib\CMB2\User_BoxLipe\Lib\CMB2\Comment_Box
Supporting types
Lipe\Lib\CMB2\Box\BoxType(enum)Lipe\Lib\CMB2\Box\TabsLipe\Lib\CMB2\Field\CheckboxLipe\Lib\CMB2\Field\Default_CallbackLipe\Lib\CMB2\Field\Display(trait)Lipe\Lib\CMB2\Field\Event_Callbacks(trait)Lipe\Lib\CMB2\Field\Field_TypeLipe\Lib\CMB2\Field\Term_Select_2Lipe\Lib\CMB2\Field\Term_Select_2\Select_2_FieldLipe\Lib\CMB2\Field\True_FalseLipe\Lib\CMB2\Field\Type(enum)Lipe\Lib\CMB2\Group\Layout(enum)Lipe\Lib\CMB2\Group\Max_RowsLipe\Lib\CMB2\Variation\CheckboxLipe\Lib\CMB2\Variation\DateLipe\Lib\CMB2\Variation\FileLipe\Lib\CMB2\Variation\OptionsLipe\Lib\CMB2\Variation\TaxonomyLipe\Lib\CMB2\Variation\TextLipe\Lib\CMB2\Variation\TextUrlLipe\Lib\CMB2\Variation\Wysiwyg
Box
The main CMB2 box wrapper. It collects fields, groups, REST settings, tabs, and display options before registering the underlying \CMB2 box.
Key public methods
public function __construct(string $id, array $object_types, ?string $title)public function add_field(Field $field): Fieldpublic function field(string $id, string $name): Field_Typepublic function group(string $id, string $name, ?string $row_title = null): Grouppublic function priority(string $priority): voidpublic function context(string $context): voidpublic function description(string $description): voidpublic function show_in_rest($methods = \WP_REST_Server::READABLE): voidpublic function add_tab(string $id, string $label): voidpublic function tabs_style(string $layout): voidpublic function remove_box_wrap(bool $remove_box_wrap = true): voidpublic function get_cmb2_box(): \CMB2public function get_box_type(): BoxType
Example
<?php
use Lipe\Lib\CMB2\Box;
$box = new Box('book-details', ['book'], 'Book Details');
$field = $box->field('isbn', 'ISBN');
$field->description('13 digit ISBN');
$box->show_in_rest();
Field
Represents a single CMB2 field and exposes shared field configuration for columns, defaults, REST behavior, display helpers, and save hooks.
Key public methods
public function column(bool|int $position = false, string $name = '', ?callable $display_cb = null, bool $disable_sorting = false): staticpublic function attributes(array $attributes): staticpublic function default(callable|string|array $default_value): staticpublic function description(string $description): staticpublic function repeatable(bool $repeatable = true, ?string $add_row_text = null): staticpublic function show_in_rest(bool|string $methods = \WP_REST_Server::ALLMETHODS): staticpublic function tab(string $id): staticpublic function set_args(Type $type, array $args, DataType $data_type): staticpublic function get_field_args(): arraypublic static function factory(string $id, string $name, Box $box, ?Group $group = null): static
Group
A repeatable group field that behaves like both a field and a field container.
Key public methods
public function field(string $id, string $name): Field_Typepublic function layout(string $layout): Grouppublic function max_rows(int $max_rows): staticpublic function repeatable(bool $repeatable = true, ?string $add_row_text = null, ?string $remove_row_text = null, ?string $remove_confirm = null): staticpublic function sortable(bool $sortable = true): staticpublic function closed(bool $closed = true): staticpublic function get_field_args(): arraypublic function add_field(Field $field): Field
Options_Page
Extends Box for CMB2-backed settings pages, including network-aware storage behavior.
Key public methods
public function __construct(string $id, ?string $title)public function capability(string $capability): voidpublic function menu_title(string $menu_title): voidpublic function parent_slug(string $parent_slug): voidpublic function network(bool $is_network = true): voidpublic function position(int $position): voidpublic function icon(string|Dashicons $icon): voidpublic function display_cb(callable $display_cb): voidpublic function save_button(?string $text): voidpublic function is_network(): bool
Example
<?php
use Lipe\Lib\CMB2\Options_Page;
$page = new Options_Page('acme-settings', 'Acme Settings');
$page->menu_title('Acme');
$page->capability('manage_options');
$page->field('api_key', 'API Key')->description('Used for upstream requests.');
Term_Box, User_Box, and Comment_Box
Specialized box subclasses for term, user, and comment screens.
Key public methods
public function __construct(string $id, array $taxonomies, string $title)(Term_Box)public function __construct(string $id, string $title)(User_Box)public function __construct(string $id, string $title)(Comment_Box)public function context(string $context): void(Comment_Box)
Field_Type
Factory exposed via Box::field() and Group::field() that returns the appropriate concrete CMB2 field type. Each method corresponds to a CMB2 field type and returns either a base Field or the matching variation builder.
Selected public methods
public function title(): Fieldpublic function text(): Textpublic function text_url(?array $protocols = null): TextUrlpublic function textarea(?int $rows = null): Textpublic function text_date(string $date_format = 'm/d/Y', string $timezone_meta_key = '', array $date_picker_options = []): Datepublic function checkbox(string $layout = Field\Checkbox::LAYOUT_BLOCK): Variation\Checkboxpublic function true_false(): Variation\Checkboxpublic function select(array|callable $options_or_callback, bool|string $show_option_none = true): Optionspublic function radio(callable|array $options_or_callback, bool|string $show_option_none = true): Optionspublic function multicheck(callable|array $options_or_callback, bool $select_all = true): Optionspublic function taxonomy_select(string $taxonomy, ?string $no_terms_text = null, ?bool $remove_default = null): Taxonomypublic function taxonomy_select_2(string $taxonomy, bool $assign_terms = false, ?string $no_terms_text = null, ?bool $remove_default = null): Taxonomypublic function wysiwyg(array $mce_options = []): Wysiwygpublic function file(?string $button_text = null, ?string $file_mime_type = null, ?bool $show_text_input = null, ?string $preview_size = null, ?string $select_text = null): Filepublic function image(string $button_text = 'Add Image', ?bool $show_text_input = null, ?string $preview_size = null): Filepublic function group(?string $title = null): Field
Additional methods cover small/medium/email text, money/number text, hidden inputs, oEmbed, color pickers, the full date/timestamp/timezone family, taxonomy radio/multicheck variants, and file_list. Refer to src/CMB2/Field/Field_Type.php for the complete list.
Variations and field helpers
The variation classes adapt a base Field into more specific fluent builders:
Variation\Dateaddspublic function date_format(string $date_format): Dateand timezone helpers.Variation\Fileaddspublic function file_query_args(Get_Posts $args): staticandpublic function preview_size(string $preview_size): static.Variation\Optionsaddspublic function options(array $options): Optionsandpublic function options_cb(callable $options_cb): Options.Variation\Taxonomyaddspublic function taxonomy_args(string $taxonomy, ?string $no_terms_text = null, ?bool $remove_default = null): arrayandpublic function get_taxonomy(): string.Variation\TextUrladdspublic function protocols(array $protocols): static.Variation\Textaddspublic function char_counter(bool $count_words = false, ?int $max = null, bool $enforce = false, array $labels = []): static.
Use Field::from() internally to translate a base field into the appropriate variation wrapper.