From e7a0f127943ca43a83eb8653344bab3d349a020f Mon Sep 17 00:00:00 2001 From: Justin Sternberg Date: Mon, 17 Oct 2016 10:31:43 -0400 Subject: [PATCH] Add cmb2_get_metabox_sanitized_values helper-function --- misc/helper-functions.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/misc/helper-functions.php b/misc/helper-functions.php index d40a713..16443d3 100644 --- a/misc/helper-functions.php +++ b/misc/helper-functions.php @@ -17,3 +17,21 @@ function cmb2_save_metabox_fields_data( $meta_box_id, $object_id, array $data_to $cmb = cmb2_get_metabox( $meta_box_id, $object_id ); $cmb->save_fields( $object_id, $cmb->object_type(), $data_to_save ); } + +/** + * Returns array of sanitized field values (without saving them) from + * provided array of values (Likely $_POST data.) + * + * Combined with the metabox `'save_fields' => false` config option, you can use this to + * save the box data somewhere else. + * + * @link https://wordpress.org/support/topic/sanitizing-data-outside-metabox-context Forum post + * + * @param mixed $meta_box_id Metabox ID (or metabox config array) + * @param int $object_id ID of post/user/comment/options-page to save the data against + * @param array $data_to_save Array of key => value data for saving. Likely $_POST data. + */ +function cmb2_get_metabox_sanitized_values( $meta_box_id, array $data_to_save ) { + $cmb = cmb2_get_metabox( $meta_box_id ); + $cmb->get_sanitized_values( $data_to_save ); +}