Examples - krunchness/CMB2 GitHub Wiki
If you didn't know, the example-functions.php contains examples for how to use CMB2 in many different contexts, including registering fields for many different object types:
- Non-post post-types. in this case, the "Page" post-type, limited to the page with the id of
2
. - User profile pages
- Taxonomy terms
- Options Page
- Comment-edit page
Comment-edit page
Add fields to the comment-edit page:
add_action( 'cmb2_admin_init', 'yourprefix_register_comment_metabox' );
/**
* Hook in and register a metabox for the admin comment edit page.
*/
function yourprefix_register_comment_metabox() {
/**
* Sample metabox to demonstrate each field type included
*/
$cmb = new_cmb2_box( array(
'id' => 'yourprefix_comment_metabox',
'title' => 'Test Metabox',
'object_types' => array( 'comment' ),
) );
$cmb->add_field( array(
'name' => 'Test Text Small',
'desc' => 'field description (optional)',
'id' => 'yourprefix_comment_textsmall',
'type' => 'text_small',
'column' => array(
'position' => 2,
'name' => 'CMB2 Custom Column',
),
) );
$cmb->add_field( array(
'name' => 'Test Color Picker',
'desc' => 'field description (optional)',
'id' => 'yourprefix_comment_colorpicker',
'type' => 'colorpicker',
'default' => '#ffffff',
'column' => array(
'position' => 2,
),
) );
}