Skip to content
This repository has been archived by the owner on May 6, 2022. It is now read-only.

Basic Usage

polupraneeth edited this page Jan 19, 2020 · 5 revisions

Table of Contents generated with DocToc

This code is designed to be run inside themes and plugins. Proper structure would look like:

/my-theme
  /cmb2-extension
  functions.php
  index.php
  screenshot.png
  styles.css

Quick Start

Open example-functions.php and copy/paste all of the code into functions.php. Create a new page and you should see all the example metaboxes in the page editor. Use get_post_meta() to get/use the data.

Getting Started

First, you need to get the bootstrap and start the engine. To do so, add the following code to functions.php. There are some caveats to including CMB2 in your plugin or theme.

Note: If you are installing the plugin from WordPress.org, you can skip this step as it is handled by the plugin.

/**
 * Get the bootstrap!
 * (Update path to use cmb2 Extension, depending on the name of the folder.
 * Case-sensitive is important on some systems.)
 */
require_once __DIR__ . '/cmb2-extension/init.php';

Notes:

  • init.php needs to be required outside any hook. It needs to be loaded as early as possible.
  • Do not do any kind of conditional loading, e.g. if ( ! class_exists..... CMB_Extension will handle that.

Create a metabox

Now that you've included the CMB2 engine, you can start adding metaboxes with the following code inside functions.php:

add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
/**
 * Define the metabox and field configurations.
 */
function cmb2_sample_metaboxes() {

	/**
	 * Initiate the metabox
	 */
	$cmb_ext_demo = new_cmb2_box( array(
		'id'            => $prefix . 'metabox',
		'title'         => esc_html__( 'Test Metabox', 'cmb2-ext' ),
		'object_types'  => array( 'page' ), // Post type
		// 'show_names' => true, // Show field names on the left
		// 'cmb_styles' => false, // false to disable the CMB stylesheet
		'tabs'      => array(
            'general'  => array(
                'label' => __('General', 'cmb2-ext'),
                'icon'  => 'dashicons-list-view', // Dashicon
            )
        ),
	) );

	
	// Switch Button
	$cmb_ext_demo->add_field( array(
           'name'             => esc_html__( 'Switch Button', 'cmb2-ext' ),
           'desc'             => esc_html__('Field description (optional)','cmb2-ext'),
           'id'               => $prefix . 'switch_button',
           'type'	      => 'switch_button',
           'tab'  	      => 'general',
           'default'          => 'off' //If it's checked by default 
         ) );
	// Add other metaboxes as needed

}

Note: For more metabox examples, see example-functions.php

Here is a cool video overview of what you will get with the example-functions.php file:

![video overview of what you will get with the example-functions.php file]

Creating the metabox using a plugin

You can also create the metabox by creating a standard WordPress plugin and simply pasting the above code below the header of the plugin.