Custom Metaboxes and Fields (CMB for short) will create metaboxes and forms with custom fields that will blow your mind.



...
'fields' => array(
array(
'name' => __( 'Test Text', 'cmb' ),
'desc' => __( 'field description (optional)', 'cmb' ),
'id' => $prefix . 'test_text',
'type' => 'text',
'sanitization_cb' => 'my_custom_sanitization', // custom sanitization callback parameter
'escape_cb' => 'my_custom_escaping', // custom escaping callback parameter
'on_front' => false, // Optionally designate a field to wp-admin only
'repeatable' => true,
),
array(
'name' => __( 'Test wysiwyg', 'cmb' ),
'desc' => __( 'field description (optional)', 'cmb' ),
'id' => $prefix . 'test_wysiwyg',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
),
),
...
How does one begin this magical journey? (example-functions.php)
add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );
function cmb_sample_metaboxes( $meta_boxes = array() ) {
// Start with an underscore to hide fields from custom fields list
$prefix = '_cmb_';
/**
* Sample metabox to demonstrate each field type included
*/
$meta_boxes['test_metabox'] = array(
'id' => 'test_metabox',
'title' => __( 'Test Metabox', 'cmb' ),
'pages' => array( 'page', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
...
),
);
// Add other metaboxes as needed
return $meta_boxes;
}
array(
'name' => __( 'Custom Type Example', 'cmb' ),
'desc' => __( 'field description (optional)', 'cmb' ),
'id' => $prefix . 'test_my_custom_type',
'type' => 'my_custom_type',
),
add_action( 'cmb_render_my_custom_type', 'cmb_render_my_custom_type', 10, 6 );
function cmb_render_my_custom_type( $args, $escaped_val, $object_id, $object_type, $cmb_types ) {
$name = $cmb_types->_name();
$id = $this->_id();
echo '<input type="text" name="'. $name .'" id="'. $id .'" value="'. $escaped_val .'" />';
echo $this->_desc( true );
}
name: The field labeldesc: Field description. Usually under or adjacent to the field input.id: The data key. If using for posts, will be the post-meta key. If using for an options page, will be the array key.type: What makes the whole thing work.repeatable: Supported by most, and will make the individual field a repeatable one.default: Specify a default value for the field.show_names: Hide label for the field.options: For fields that take an options array. These include: select, radio, multicheck, wysiwyg and group.before and after: These allow you to add arbitrary text/markup before and after the field just inside the td tag.on_front: If you’re planning on using your metabox fields on the front-end as well (user-facing), then you can specify that certain fields do not get displayed there by setting this parameter to false.attributes: Will modify default attributes (class, input type, rows, etc), or add your own (placeholder, data attributes). Example:
array(
'name' => 'Extra Small Textarea',
'id' => $prefix .'xtra_small_textarea',
'type' => 'textarea_small',
'attributes' => array(
'placeholder' => 'A small amount of text',
'rows' => 3,
'class' => 'custom-class-name',
),
),
escape_cb: Bypass the CMB escaping (escapes before display) methods with your own callback.sanitization_cb: Bypass the CMB sanitization (sanitizes before saving) methods with your own callback.cmb_before_tablecmb_after_tablecmb_render_$namecmb_show_oncmb_meta_box_url: tell cmb where it livescmb_allow_frontend: Disallow a form/metabox on the front-endcmb_override_meta_value