-
Notifications
You must be signed in to change notification settings - Fork 420
Basic Usage
This code is designed to be run inside themes and plugins. I typically put it inside [theme or plugin]/lib/metabox, but you can put it wherever you like. For this example it will be in child-theme/lib/metabox.You can then use it within your theme or plugin. For this example, my customizations are going in child-theme/functions.php.
First you add a filter to 'cmb_meta_boxes' that adds your metabox to the $meta_boxes array. It is also a good idea to define a $prefix variable.
<?php
$prefix = '_cmb_'; // Prefix for all fields
function be_sample_metaboxes( $meta_boxes ) {
global $prefix;
$meta_boxes[] = array(
'id' => 'test_metabox',
'title' => 'Test Metabox',
'pages' => array('page'), // post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Test Text',
'desc' => 'field description (optional)',
'id' => $prefix . 'test_text',
'type' => 'text'
),
),
);
return $meta_boxes;
}
add_filter( 'cmb_meta_boxes', 'be_sample_metaboxes' );
This creates a metabox titled "Text Metabox" that shows up on all pages and contains a single text field. The important things to note are that you're adding to the existing $meta_boxes array ( $meta_boxes[] = ...
) and at the end you're returning the whole array.
Once you've set up your metaboxes, you'll need to add the initialization function:
<?php
// Initialize the metabox class
add_action( 'init', 'be_initialize_cmb_meta_boxes', 9999 );
function be_initialize_cmb_meta_boxes() {
if ( !class_exists( 'cmb_Meta_Box' ) ) {
require_once( 'lib/metabox/init.php' );
}
}
The important things to note here are that you're first checking to see if the cmb_Meta_Box
class exists, and if it doesn't you're calling the init.php file. Make sure you're linking to the file correctly. I typically use a theme or plugin constant (not shown in above code).