cmb2_get_field

Get a CMB2 field object.

Description

(CMB2_Field|null) cmb2_get_field( (array) $meta_box, (array) $field_id, (int) $object_id = 0, (string) $object_type = '' ); 

Returns (CMB2_Field|null)

CMB2_Field object unless metabox config cannot be found

Parameters (4)

0. $meta_box (array)
Metabox ID or Metabox config array
1. $field_id (array)
Field ID or all field arguments
2. $object_id — Optional. (int)
The object id.
3. $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_field' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'cmb2/includes/helper-functions.php'; 
  3.  
  4. // Metabox ID or Metabox config array 
  5. $meta_box = array(); 
  6.  
  7. // Field ID or all field arguments 
  8. $field_id = array(); 
  9.  
  10. // The object id. 
  11. $object_id = -1; 
  12.  
  13. // Type of object being saved. (e.g., post, user, comment, or options-page). 
  14. // Defaults to metabox object type. 
  15. $object_type = ''; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = cmb2_get_field($meta_box, $field_id, $object_id, $object_type); 
  19.  

Defined (1)

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

/includes/helper-functions.php  
  1. function cmb2_get_field( $meta_box, $field_id, $object_id = 0, $object_type = '' ) { 
  2.  
  3. $object_id = $object_id ? $object_id : get_the_ID(); 
  4. $cmb = $meta_box instanceof CMB2 ? $meta_box : cmb2_get_metabox( $meta_box, $object_id ); 
  5.  
  6. if ( ! $cmb ) { 
  7. return; 
  8.  
  9. $cmb->object_type( $object_type ? $object_type : $cmb->mb_object_type() ); 
  10.  
  11. return $cmb->get_field( $field_id );