cmb2_get_metabox

Retrieve a CMB2 instance by the metabox ID.

Description

cmb2_get_metabox( (mixed) $meta_box, (int) $object_id = 0, (string) $object_type = '' ); 

Parameters (3)

0. $meta_box (mixed)
Metabox ID or Metabox config array
1. $object_id — Optional. (int)
The object id.
2. $object_type — Optional. (string) => ''
Type of object being saved. (e.g., post, user, comment, or options-page). Defaults to metabox object type.

Usage

  1. if ( !function_exists( 'cmb2_get_metabox' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'cmb2/includes/helper-functions.php'; 
  3.  
  4. // Metabox ID or Metabox config array 
  5. $meta_box = null; 
  6.  
  7. // The object id. 
  8. $object_id = -1; 
  9.  
  10. // Type of object being saved. (e.g., post, user, comment, or options-page). 
  11. // Defaults to metabox object type. 
  12. $object_type = ''; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = cmb2_get_metabox($meta_box, $object_id, $object_type); 
  16.  

Defined (1)

The function is defined in the following location(s).

/includes/helper-functions.php  
  1. function cmb2_get_metabox( $meta_box, $object_id = 0, $object_type = '' ) { 
  2.  
  3. if ( $meta_box instanceof CMB2 ) { 
  4. return $meta_box; 
  5.  
  6. if ( is_string( $meta_box ) ) { 
  7. $cmb = CMB2_Boxes::get( $meta_box ); 
  8. } else { 
  9. // See if we already have an instance of this metabox 
  10. $cmb = CMB2_Boxes::get( $meta_box['id'] ); 
  11. // If not, we'll initate a new metabox 
  12. $cmb = $cmb ? $cmb : new CMB2( $meta_box, $object_id ); 
  13.  
  14. if ( $cmb && $object_id ) { 
  15. $cmb->object_id( $object_id ); 
  16.  
  17. if ( $cmb && $object_type ) { 
  18. $cmb->object_type( $object_type ); 
  19.  
  20. return $cmb;