CMB2::save_fields PHP Method

save_fields() public method

Loops through and saves field data
Since: 1.0.0
public save_fields ( integer $object_id, string $object_type = '', array $data_to_save = [] )
$object_id integer Object ID
$object_type string Type of object being saved. (e.g., post, user, or comment)
$data_to_save array Array of key => value data for saving. Likely $_POST data.
    public function save_fields($object_id = 0, $object_type = '', $data_to_save = array())
    {
        // Fall-back to $_POST data
        $this->data_to_save = !empty($data_to_save) ? $data_to_save : $_POST;
        $object_id = $this->object_id($object_id);
        $object_type = $this->object_type($object_type);
        $this->process_fields();
        // If options page, save the updated options
        if ('options-page' == $object_type) {
            cmb2_options($object_id)->set();
        }
        $this->after_save();
    }

Usage Example

 /**
  * Handles saving of the $_POST data
  * @since  0.1.3
  * @param  int $term_id Term's ID
  */
 public function do_save($term_id)
 {
     if (!class_exists('CMB2')) {
         return;
     }
     $object_id = $this->id($term_id);
     if (isset($_POST[$this->cmb->nonce()]) && wp_verify_nonce($_POST[$this->cmb->nonce()], $this->cmb->nonce())) {
         $this->do_override_filters($term_id);
         $this->cmb->save_fields($object_id, 'options-page', $_POST);
     }
 }