Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created June 6, 2018 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/6b6eb25bfc4e095d9fd9b6bd72580d57 to your computer and use it in GitHub Desktop.
Save billerickson/6b6eb25bfc4e095d9fd9b6bd72580d57 to your computer and use it in GitHub Desktop.
<?php
/**
* Custom Fields with CMB2
*
* @package CoreFunctionality
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
**/
/**
* Define the metabox and field configurations.
*/
function be_register_cmb2_metaboxes() {
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box( array(
'id' => 'be_plugins_metabox',
'title' => __( 'Plugins' ),
'object_types' => array( 'page' ),
'show_on' => array( 'key' => 'page-template', 'value' => 'templates/plugins-cmb2.php' ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
// 'cmb_styles' => false, // false to disable the CMB stylesheet
// 'closed' => true, // Keep the metabox closed by default
) );
$group_field_id = $cmb->add_field( array(
'id' => 'be_plugins',
'type' => 'group',
'options' => array(
'group_title' => __( 'Plugin {#}' ),
'add_button' => __( 'Add Another Plugin' ),
'remove_button' => __( 'Remove Plugin' ),
'sortable' => true,
)
));
$cmb->add_group_field( $group_field_id, array(
'name' => 'Name',
'id' => 'name',
'type' => 'text',
));
$cmb->add_group_field( $group_field_id, array(
'name' => 'URL',
'id' => 'url',
'type' => 'text',
));
$cmb->add_group_field( $group_field_id, array(
'name' => 'Icon',
'id' => 'icon',
'type' => 'text',
));
$cmb->add_group_field( $group_field_id, array(
'name' => 'Summary',
'id' => 'summary',
'type' => 'textarea',
));
$cmb->add_group_field( $group_field_id, array(
'name' => 'Categories',
'id' => 'categories',
'type' => 'multicheck_inline',
'options' => ea_plugin_categories(),
));
}
add_action( 'cmb2_admin_init', 'be_register_cmb2_metaboxes' );
/**
* Plugin Categories
*
*/
function ea_plugin_categories() {
return array(
'genesis' => 'Genesis',
'developer' => 'Developer',
'cms' => 'CMS',
'social' => 'Social',
'media' => 'Media',
'widget' => 'Widget',
'analytics' => 'Analytics'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment