cmb2_print_metabox_form

Display a metabox form & save it on submission.

Description

cmb2_print_metabox_form( (mixed) $meta_box, (int) $object_id = 0, (array) $args = array() ); 

Parameters (3)

0. $meta_box (mixed)
Metabox config array or Metabox ID
1. $object_id — Optional. (int)
The object id.
2. $args — Optional. (array) => array()
Arguments array

Usage

  1. if ( !function_exists( 'cmb2_print_metabox_form' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'cmb2/includes/helper-functions.php'; 
  3.  
  4. // Metabox config array or Metabox ID 
  5. $meta_box = null; 
  6.  
  7. // The object id. 
  8. $object_id = -1; 
  9.  
  10. // Optional arguments array 
  11. $args = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = cmb2_print_metabox_form($meta_box, $object_id, $args); 
  15.  

Defined (1)

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

/includes/helper-functions.php  
  1. function cmb2_print_metabox_form( $meta_box, $object_id = 0, $args = array() ) { 
  2.  
  3. $object_id = $object_id ? $object_id : get_the_ID(); 
  4. $cmb = cmb2_get_metabox( $meta_box, $object_id ); 
  5.  
  6. // if passing a metabox ID, and that ID was not found 
  7. if ( ! $cmb ) { 
  8. return; 
  9.  
  10. $args = wp_parse_args( $args, array( 
  11. 'form_format' => '<form class="cmb-form" method="post" id="%1$s" enctype="multipart/form-data" encoding="multipart/form-data"><input type="hidden" name="object_id" value="%2$s">%3$s<input type="submit" name="submit-cmb" value="%4$s" class="button-primary"></form>',  
  12. 'save_button' => esc_html__( 'Save', cmb2 ),  
  13. 'object_type' => $cmb->mb_object_type(),  
  14. 'cmb_styles' => $cmb->prop( 'cmb_styles' ),  
  15. 'enqueue_js' => $cmb->prop( 'enqueue_js' ),  
  16. ) ); 
  17.  
  18. // Set object type explicitly (rather than trying to guess from context) 
  19. $cmb->object_type( $args['object_type'] ); 
  20.  
  21. // Save the metabox if it's been submitted 
  22. // check permissions 
  23. // @todo more hardening? 
  24. if ( 
  25. $cmb->prop( 'save_fields' ) 
  26. // check nonce 
  27. && isset( $_POST['submit-cmb'], $_POST['object_id'], $_POST[ $cmb->nonce() ] ) 
  28. && wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() ) 
  29. && $object_id && $_POST['object_id'] == $object_id 
  30. ) { 
  31. $cmb->save_fields( $object_id, $cmb->object_type(), $_POST ); 
  32.  
  33. // Enqueue JS/CSS 
  34. if ( $args['cmb_styles'] ) { 
  35. CMB2_hookup::enqueue_cmb_css(); 
  36.  
  37. if ( $args['enqueue_js'] ) { 
  38. CMB2_hookup::enqueue_cmb_js(); 
  39.  
  40. $form_format = apply_filters( 'cmb2_get_metabox_form_format', $args['form_format'], $object_id, $cmb ); 
  41.  
  42. $format_parts = explode( '%3$s', $form_format ); 
  43.  
  44. // Show cmb form 
  45. printf( $format_parts[0], $cmb->cmb_id, $object_id ); 
  46. $cmb->show_form(); 
  47.  
  48. if ( isset( $format_parts[1] ) && $format_parts[1] ) { 
  49. printf( str_ireplace( '%4$s', '%1$s', $format_parts[1] ), $args['save_button'] ); 
  50.