CMB2 Documentation
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  • Hooks
  • Download Docs
  • Github

Packages

  • CMB2
  • Demo
    • CMB2
  • None
  • Tests
    • CMB2

Classes

  • CMB2
  • CMB2_Ajax
  • CMB2_Base
  • CMB2_Bootstrap_2101
  • CMB2_Boxes
  • CMB2_Display_Checkbox
  • CMB2_Display_Colorpicker
  • CMB2_Display_File
  • CMB2_Display_File_List
  • CMB2_Display_Multicheck
  • CMB2_Display_oEmbed
  • CMB2_Display_Select
  • CMB2_Display_Taxonomy_Multicheck
  • CMB2_Display_Taxonomy_Radio
  • CMB2_Display_Text_Date
  • CMB2_Display_Text_Date_Timezone
  • CMB2_Display_Text_Money
  • CMB2_Display_Text_Time
  • CMB2_Display_Text_Url
  • CMB2_Display_Textarea
  • CMB2_Display_Textarea_Code
  • CMB2_Field
  • CMB2_Field_Display
  • CMB2_Hookup
  • CMB2_Hookup_Base
  • CMB2_JS
  • CMB2_Option
  • CMB2_Options
  • CMB2_Options_Hookup
  • CMB2_REST
  • CMB2_REST_Controller
  • CMB2_REST_Controller_Boxes
  • CMB2_REST_Controller_Fields
  • CMB2_Sanitize
  • CMB2_Show_Filters
  • CMB2_Type_Base
  • CMB2_Type_Checkbox
  • CMB2_Type_Colorpicker
  • CMB2_Type_Counter_Base
  • CMB2_Type_File
  • CMB2_Type_File_Base
  • CMB2_Type_File_List
  • CMB2_Type_Multi_Base
  • CMB2_Type_Multicheck
  • CMB2_Type_Oembed
  • CMB2_Type_Picker_Base
  • CMB2_Type_Radio
  • CMB2_Type_Select
  • CMB2_Type_Select_Timezone
  • CMB2_Type_Taxonomy_Base
  • CMB2_Type_Taxonomy_Multicheck
  • CMB2_Type_Taxonomy_Multicheck_Hierarchical
  • CMB2_Type_Taxonomy_Radio
  • CMB2_Type_Taxonomy_Radio_Hierarchical
  • CMB2_Type_Taxonomy_Select
  • CMB2_Type_Taxonomy_Select_Hierarchical
  • CMB2_Type_Text
  • CMB2_Type_Text_Date
  • CMB2_Type_Text_Datetime_Timestamp
  • CMB2_Type_Text_Datetime_Timestamp_Timezone
  • CMB2_Type_Text_Time
  • CMB2_Type_Textarea
  • CMB2_Type_Textarea_Code
  • CMB2_Type_Title
  • CMB2_Type_Wysiwyg
  • CMB2_Types
  • CMB2_Utils
  • Test_CMB2_Utils_WIN

Functions

  • _tests_cmb2_manually_load_plugin
  • cmb2_ajax
  • cmb2_autoload_classes
  • cmb2_bootstrap
  • cmb2_dir
  • cmb2_do_oembed
  • cmb2_get_field
  • cmb2_get_field_value
  • cmb2_get_metabox
  • cmb2_get_metabox_form
  • cmb2_get_metabox_sanitized_values
  • cmb2_get_oembed
  • cmb2_get_option
  • cmb2_metabox_form
  • cmb2_options
  • cmb2_print_metabox_form
  • cmb2_update_option
  • cmb2_utils
  • date_create_from_format
  • date_timestamp_get
  • new_cmb2_box

Hooks

  • Hook Reference

Class CMB2

The main CMB2 object for storing box data/properties.

CMB2_Base
Extended by CMB2

Direct known subclasses

Test_CMB2_Object

Package: CMB2
License: GPL-2.0+
Author: CMB2 team
Located at includes/CMB2.php

Methods summary

public
# __construct( array $config, integer $object_id = 0 )

Get started

Get started

Parameters

$config
Metabox config array.
$object_id
Optional object id.

Since

0.4.0

Overrides

CMB2_Base::__construct
public CMB2
# show_form( integer $object_id = 0, string $object_type = '' )

Loops through and displays fields

Loops through and displays fields

Parameters

$object_id
Object ID.
$object_type
Type of object being saved. (e.g., post, user, or comment).

Returns

CMB2

Since

1.0.0
public CMB2
# render_form_open( integer $object_id = 0, string $object_type = '' )

Outputs the opening form markup and runs corresponding hooks: 'cmb2_before_form' and "cmb2_before_{$object_type}form{$this->cmb_id}"

Outputs the opening form markup and runs corresponding hooks: 'cmb2_before_form' and "cmb2_before_{$object_type}form{$this->cmb_id}"

Parameters

$object_id
Object ID.
$object_type
Object type.

Returns

CMB2

Since

2.2.0
public string
# box_classes( )

Defines the classes for the CMB2 form/wrap.

Defines the classes for the CMB2 form/wrap.

Returns

string
Space concatenated list of classes

Since

2.0.0
public CMB2
# render_form_close( integer $object_id = 0, string $object_type = '' )

Outputs the closing form markup and runs corresponding hooks: 'cmb2_after_form' and "cmb2_after_{$object_type}form{$this->cmb_id}"

Outputs the closing form markup and runs corresponding hooks: 'cmb2_after_form' and "cmb2_after_{$object_type}form{$this->cmb_id}"

Parameters

$object_id
Object ID.
$object_type
Object type.

Returns

CMB2

Since

2.2.0
public mixed
# render_field( array $field_args )

Renders a field based on the field type

Renders a field based on the field type

Parameters

$field_args
A field configuration array.

Returns

mixed
CMB2_Field object if successful.

Since

2.2.0
public CMB2_Field|null
# render_group( array|CMB2_Field $args )

Render a group of fields.

Render a group of fields.

Parameters

$args
Array of field arguments for a group field parent or the group parent field.

Returns

CMB2_Field|null
Group field object.
public CMB2_Field|null
# render_group_callback( array $field_args, CMB2_Field $field_group )

The default callback to render a group of fields.

The default callback to render a group of fields.

Parameters

$field_args
Array of field arguments for the group field parent.
$field_group
The CMB2_Field group object.

Returns

CMB2_Field|null
Group field object.

Since

2.2.6
public string
# group_wrap_attributes( CMB2_Field $field_group )

Get the group wrap attributes, which are passed through a filter.

Get the group wrap attributes, which are passed through a filter.

Parameters

$field_group
The group CMB2_Field object.

Returns

string
The attributes string.

Since

2.2.3
public CMB2
# render_group_row( CMB2_Field $field_group )

Render a repeatable group row

Render a repeatable group row

Parameters

$field_group
CMB2_Field group field object.

Returns

CMB2

Since

1.0.2
public CMB2_Field
# add_hidden_field( array $field_args, CMB2_Field|null $field_group = null )

Add a hidden field to the list of hidden fields to be rendered later.

Add a hidden field to the list of hidden fields to be rendered later.

Parameters

$field_args
Array of field arguments to be passed to CMB2_Field.
$field_group
CMB2_Field group field object.

Returns

CMB2_Field

Since

2.0.0
public CMB2
# render_hidden_fields( )

Loop through and output hidden fields

Loop through and output hidden fields

Returns

CMB2

Since

2.0.0
public mixed
# get_sanitized_values( array $data_to_sanitize )

Returns array of sanitized field values (without saving them)

Returns array of sanitized field values (without saving them)

Parameters

$data_to_sanitize
Array of field_id => value data for sanitizing (likely $_POST data).

Returns

mixed

Since

2.0.3
public CMB2
# save_fields( integer $object_id = 0, string $object_type = '', array $data_to_save = array() )

Loops through and saves field data

Loops through and saves field data

Parameters

$object_id
Object ID.
$object_type
Type of object being saved. (e.g., post, user, or comment).
$data_to_save
Array of key => value data for saving. Likely $_POST data.

Returns

CMB2

Since

1.0.0
public CMB2
# process_fields( )

Process and save form fields

Process and save form fields

Returns

CMB2

Since

2.0.0
public CMB2
# process_field( array $field_args )

Process and save a field

Process and save a field

Parameters

$field_args
Array of field arguments.

Returns

CMB2

Since

2.0.0
public CMB2
# pre_process( )

Fires the "cmb2_{$object_type}process_fields{$cmb_id}" action hook.

Fires the "cmb2_{$object_type}process_fields{$cmb_id}" action hook.

Returns

CMB2

Since

2.2.2
public CMB2
# after_save( )

Fires the "cmb2_save_{$object_type}fields" and "cmb2_save{$object_type}fields{$cmb_id}" action hooks.

Fires the "cmb2_save_{$object_type}fields" and "cmb2_save{$object_type}fields{$cmb_id}" action hooks.

Returns

CMB2

Since

2.x.x
public mixed
# save_group( array $args )

Save a repeatable group

Save a repeatable group

Parameters

$args
Field arguments array.

Returns

mixed
Return of CMB2_Field::update_data().

Since

1.x.x
public mixed
# save_group_field( CMB2_Field $field_group )

Save a repeatable group

Save a repeatable group

Parameters

$field_group
CMB2_Field group field object.

Returns

mixed
Return of CMB2_Field::update_data().

Since

1.x.x
public integer|string
# object_id( integer|string $object_id = 0 )

Get object id from global space if no id is provided

Get object id from global space if no id is provided

Parameters

$object_id
Object ID.

Returns

integer|string
$object_id Object ID.

Since

1.0.0

Overrides

CMB2_Base::object_id
public string
# mb_object_type( )

Sets the $object_type based on metabox settings

Sets the $object_type based on metabox settings

Returns

string
Object type.

Since

1.0.0
public array
# box_types( array $fallback = array() )

Gets the box 'object_types' array based on box settings.

Gets the box 'object_types' array based on box settings.

Parameters

$fallback
Fallback value.

Returns

array
Object types.

Since

2.2.3
public boolean
# is_box_type( string|array $object_types = array(), array $fallback = array() )

Check if given object_type(s) matches any of the registered object types or taxonomies for this box.

Check if given object_type(s) matches any of the registered object types or taxonomies for this box.

Parameters

$object_types
The object type(s) to check.
$fallback
Fallback object_types value.

Returns

boolean
Whether given object type(s) are registered to this box.

Since

2.7.0
public
# init_options_mb( )

Initates the object types and option key for an options page metabox.

Initates the object types and option key for an options page metabox.

Since

2.2.5
protected array
# deinit_options_mb( array $types )

If object-page initiation failed, remove traces options page setup.

If object-page initiation failed, remove traces options page setup.

Parameters

$types
Array of types.

Returns

array

Since

2.2.5
public boolean
# is_options_page_mb( )

Determines if metabox is for an options page

Determines if metabox is for an options page

Returns

boolean
True/False.

Since

1.0.1
public boolean
# is_old_school_options_page_mb( )

Determines if metabox uses old-schoold options page config.

Determines if metabox uses old-schoold options page config.

Returns

boolean
True/False.

Since

2.2.5
public boolean
# doing_options_page( )

Determine if we are on an options page (or saving the options page).

Determine if we are on an options page (or saving the options page).

Returns

boolean

Since

2.2.5
public string|array
# options_page_keys( )

Get the options page key.

Get the options page key.

Returns

string|array

Since

2.2.5
public string
# object_type( string $object_type = '' )

Returns the object type

Returns the object type

Parameters

$object_type
Type of object being saved. (e.g., post, user, or comment). Optional.

Returns

string
Object type.

Since

1.0.0

Overrides

CMB2_Base::object_type
public string
# current_object_type( )

Get the object type for the current page, based on the $pagenow global.

Get the object type for the current page, based on the $pagenow global.

Returns

string
Page object type name.

Since

2.2.2

Overrides

CMB2_Base::current_object_type
public mixed
# set_prop( string $property, mixed $value )

Set metabox property.

Set metabox property.

Parameters

$property
Metabox config property to retrieve.
$value
Value to set if no value found.

Returns

mixed
Metabox config property value or false.

Since

2.2.2

Overrides

CMB2_Base::set_prop
public mixed
# prop( string $property, mixed $fallback = null )

Get metabox property and optionally set a fallback

Get metabox property and optionally set a fallback

Parameters

$property
Metabox config property to retrieve.
$fallback
Fallback value to set if no value found.

Returns

mixed
Metabox config property value or false.

Since

2.0.0

Overrides

CMB2_Base::prop
public CMB2_Field|false
# get_field( string|array|CMB2_Field $field, CMB2_Field|null $field_group = null, boolean $reset_cached = false )

Get a field object

Get a field object

Parameters

$field
Metabox field id or field config array or CMB2_Field object.
$field_group
(optional) CMB2_Field object (group parent).
$reset_cached

(optional) Reset the internal cache for this field object. Use sparingly.

Returns

CMB2_Field|false
CMB2_Field object (or false).

Since

2.0.3
public array
# get_field_args( mixed $field_id, mixed $field_args, mixed $sub_field_id, CMB2_Field|null $field_group )

Handles determining which type of arguments to pass to CMB2_Field

Handles determining which type of arguments to pass to CMB2_Field

Parameters

$field_id
Field (or group field) ID.
$field_args
Array of field arguments.
$sub_field_id
Sub field ID (if field_group exists).
$field_group
If a sub-field, will be the parent group CMB2_Field object.

Returns

array
Array of CMB2_Field arguments.

Since

2.0.7
protected array
# get_default_args( array $field_args, CMB2_Field $field_group = null )

Get default field arguments specific to this CMB2 object.

Get default field arguments specific to this CMB2 object.

Parameters

$field_args
Metabox field config array.
$field_group
(optional) CMB2_Field object (group parent).

Returns

array
Array of field arguments.

Since

2.2.0

Overrides

CMB2_Base::get_default_args
protected CMB2
# add_fields( array $fields, mixed $parent_field_id = null )

When fields are added in the old-school way, intitate them as they should be

When fields are added in the old-school way, intitate them as they should be

Parameters

$fields
Array of fields to add.
$parent_field_id
Parent field id or null.

Returns

CMB2

Since

2.1.0
public string|false
# add_field( array $field, integer $position = 0 )

Add a field to the metabox

Add a field to the metabox

Parameters

$field
Metabox field config array.
$position
(optional) Position of metabox. 1 for first, etc.

Returns

string|false
Field id or false.

Since

2.0.0
public mixed
# add_group_field( string $parent_field_id, array $field, integer $position = 0 )

Add a field to a group

Add a field to a group

Parameters

$parent_field_id
The field id of the group field to add the field.
$field
Metabox field config array.
$position
(optional) Position of metabox. 1 for first, etc.

Returns

mixed
Array of parent/field ids or false.

Since

2.0.0
protected
# field_actions( array $field )

Perform some field-type-specific initiation actions.

Perform some field-type-specific initiation actions.

Parameters

$field
Metabox field config array.

Since

2.7.0
protected array
# define_field_column( array $field )

Defines a field's column if requesting to be show in admin columns.

Defines a field's column if requesting to be show in admin columns.

Parameters

$field
Metabox field config array.

Returns

array
Modified metabox field config array.

Since

2.2.3
protected
# _add_field_to_array( array $field, array & $fields, integer $position = 0 )

Add a field array to a fields array in desired position

Add a field array to a fields array in desired position

Parameters

$field
Metabox field config array.
$fields
Array (passed by reference) to append the field (array) to.
$position
Optionally specify a position in the array to be inserted.

Since

2.0.2
public boolean
# remove_field( string $field_id, string $parent_field_id = '' )

Remove a field from the metabox

Remove a field from the metabox

Parameters

$field_id
The field id of the field to remove.
$parent_field_id
(optional) The field id of the group field to remove field from.

Returns

boolean
True if field was removed.

Since

2.0.0
public mixed
# update_field_property( string $field_id, string $property, mixed $value, string $parent_field_id = '' )

Update or add a property to a field

Update or add a property to a field

Parameters

$field_id
Field id.
$property
Field property to set/update.
$value
Value to set the field property.
$parent_field_id
(optional) The field id of the group field to remove field from.

Returns

mixed
Field id. Strict compare to false, as success can return a falsey value (like 0).

Since

2.0.0
public mixed
# get_field_ids( string $field_id, string $parent_field_id = '' )

Check if field ids match a field and return the index/field id

Check if field ids match a field and return the index/field id

Parameters

$field_id
Field id.
$parent_field_id
(optional) Parent field id.

Returns

mixed
Array of field/parent ids, or false.

Since

2.0.2
public mixed
# search_old_school_array( string $field_id, array $fields )

When using the old array filter, it is unlikely field array indexes will be the field id.

When using the old array filter, it is unlikely field array indexes will be the field id.

Parameters

$field_id
The field id.
$fields
Array of fields to search.

Returns

mixed
Field index or false.

Since

2.0.2
public mixed
# do_callback( callable $cb, mixed $additional_params = null )

Handles metabox property callbacks, and passes this $cmb object as property.

Handles metabox property callbacks, and passes this $cmb object as property.

Parameters

$cb
The callback method/function/closure.
$additional_params
Any additoinal parameters which should be passed to the callback.

Returns

mixed
Return of the callback function.

Since

2.2.3

Overrides

CMB2_Base::do_callback
public
# nonce_field( )

Generate a unique nonce field for each registered meta_box

Generate a unique nonce field for each registered meta_box

Since

2.0.0
public string
# nonce( )

Generate a unique nonce for each registered meta_box

Generate a unique nonce for each registered meta_box

Returns

string
unique nonce string.

Since

2.0.0
public boolean
# was_updated( )

Checks if field-saving updated any fields.

Checks if field-saving updated any fields.

Returns

boolean

Since

2.2.5
public boolean
# is_alternate_context_box( )

Whether this box is an "alternate context" box. This means the box has a 'context' property defined as: 'form_top', 'before_permalink', 'after_title', or 'after_editor'.

Whether this box is an "alternate context" box. This means the box has a 'context' property defined as: 'form_top', 'before_permalink', 'after_title', or 'after_editor'.

Returns

boolean

Since

2.2.4
public mixed
# __get( string $property )

Magic getter for our object.

Magic getter for our object.

Parameters

$property
Object property.

Returns

mixed

Throws

Exception
Throws an exception if the field is invalid.

Overrides

CMB2_Base::__get

Methods inherited from CMB2_Base

__call(), deprecated_param(), get_new_field(), get_param_callback_result(), maybe_callback(), maybe_hook(), maybe_hook_parameter(), peform_param_callback(), should_show(), unset_param_callback_cache()

Constants summary

Constants inherited from CMB2_Base

DEPRECATED_CB_PARAM, DEPRECATED_PARAM

Properties summary

protected string $properties_name

The object properties name.

The object properties name.

Since

2.2.3
# 'meta_box'
protected array $meta_box

Metabox Config array

Metabox Config array

Since

0.9.0
# array()
protected string $mb_object_type

Type of object registered for metabox. (e.g., post, user, or comment)

Type of object registered for metabox. (e.g., post, user, or comment)

Since

1.0.0
# null
protected array $updated

List of fields that are changed/updated on save

List of fields that are changed/updated on save

Since

1.1.0
# array()
protected array $mb_defaults

Metabox Defaults

Metabox Defaults

Since

1.0.1
# array( 'id' => '', 'title' => '', // Post type slug, or 'user', 'term', 'comment', or 'options-page'. 'object_types' => array(), /** * The context within the screen where the boxes should display. Available contexts vary * from screen to screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. * * For placement in locations outside of a metabox, other options include: * 'form_top', 'before_permalink', 'after_title', 'after_editor' * * Comments screen contexts include 'normal' and 'side'. Default is 'normal'. */ 'context' => 'normal', 'priority' => 'high', // Or 10 for options pages. 'show_names' => true, // Show field names on the left. 'show_on_cb' => null, // Callback to determine if metabox should display. 'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb'. 'cmb_styles' => true, // Include CMB2 stylesheet. 'enqueue_js' => true, // Include CMB2 JS. 'fields' => array(), /** * Handles hooking CMB2 forms/metaboxes into the post/attachement/user/options-page screens * and handles hooking in and saving those fields. */ 'hookup' => true, 'save_fields' => true, // Will not save during hookup if false. 'closed' => false, // Default metabox to being closed. 'taxonomies' => array(), 'new_user_section' => 'add-new-user', // or 'add-existing-user'. 'new_term_section' => true, 'show_in_rest' => false, 'classes' => null, // Optionally add classes to the CMB2 wrapper. 'classes_cb' => '', // Optionally add classes to the CMB2 wrapper (via a callback). /* * The following parameter is for post alternate-context metaboxes only. * * To output the fields 'naked' (without a postbox wrapper/style), then * add a `'remove_box_wrap' => true` to your metabox registration array. */ 'remove_box_wrap' => false, /* * The following parameter is any additional arguments passed as $callback_args * to add_meta_box, if/when applicable. * * CMB2 does not use these arguments in the add_meta_box callback, however, these args * are parsed for certain special properties, like determining Gutenberg/block-editor * compatibility. * * Examples: * * - Make sure default editor is used as metabox is not compatible with block editor * [ '__block_editor_compatible_meta_box' => false/true ] * * - Or declare this box exists for backwards compatibility * [ '__back_compat_meta_box' => false ] * * More: https://wordpress.org/gutenberg/handbook/extensibility/meta-box/ */ 'mb_callback_args' => null, /* * The following parameters are for options-page metaboxes, * and several are passed along to add_menu_page()/add_submenu_page() */ // 'menu_title' => null, // Falls back to 'title' (above). Do not define here so we can set a fallback. 'message_cb' => '', // Optionally define the options-save message (via a callback). 'option_key' => '', // The actual option key and admin menu page slug. 'parent_slug' => '', // Used as first param in add_submenu_page(). 'capability' => 'manage_options', // Cap required to view options-page. 'icon_url' => '', // Menu icon. Only applicable if 'parent_slug' is left empty. 'position' => null, // Menu position. Only applicable if 'parent_slug' is left empty. 'admin_menu_hook' => 'admin_menu', // Alternately 'network_admin_menu' to add network-level options page. 'display_cb' => false, // Override the options-page form output (CMB2_Hookup::options_page_output()). 'save_button' => '', // The text for the options-page save button. Defaults to 'Save'. 'disable_settings_errors' => false, // On settings pages (not options-general.php sub-pages), allows disabling. 'tab_group' => '', // Tab-group identifier, enables options page tab navigation. // 'tab_title' => null, // Falls back to 'title' (above). Do not define here so we can set a fallback. // 'autoload' => true, // Defaults to true, the options-page option will be autloaded. )
protected array $fields

Metabox field objects

Metabox field objects

Since

2.0.3
# array()
protected array $hidden_fields

An array of hidden fields to output at the end of the form

An array of hidden fields to output at the end of the form

Since

2.0.0
# array()
protected string $generated_nonce

Array of key => value data for saving. Likely $_POST data.

Array of key => value data for saving. Likely $_POST data.

Since

2.0.0
# ''
protected boolean $has_columns

Whether there are fields to be shown in columns. Set in CMB2::add_field().

Whether there are fields to be shown in columns. Set in CMB2::add_field().

Since

2.2.2
# false
protected array $tax_metaboxes_to_remove

If taxonomy field is requesting to remove_default, we store the taxonomy here.

If taxonomy field is requesting to remove_default, we store the taxonomy here.

Since

2.2.3
# array()

Properties inherited from CMB2_Base

$callback_results, $cmb_id, $data_to_save, $object_id, $object_type

Magic properties inherited from CMB2_Base

$args, $cmb_id, $meta_box, $object_id, $object_type, $properties

CMB2 Documentation API documentation generated by ApiGen