Title: Using CMB2 in a Plugin?
Last modified: August 30, 2016

---

# Using CMB2 in a Plugin?

 *  Resolved [Dave Mackey](https://wordpress.org/support/users/davidshq/)
 * (@davidshq)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/using-cmb2-in-a-plugin/)
 * I’m trying to use CMB2 with a plugin, but all the documentation I’m finding is
   about using it with themes. For example, in the wiki it states,
    “Open example-
   functions.php and copy/paste all of the code into functions.php.”
 * How does this work when I am seeking to add this functionality into a plugin?
   I don’t have a functions.php file?
 * [https://wordpress.org/plugins/cmb2/](https://wordpress.org/plugins/cmb2/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [Justin Sternberg](https://wordpress.org/support/users/jtsternberg/)
 * (@jtsternberg)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/using-cmb2-in-a-plugin/#post-6375370)
 * Probably best to see an example in a plugin:
    [https://github.com/WebDevStudios/WDS-Simple-Page-Builder/blob/master/wds-simple-page-builder.php#L35-L38](https://github.com/WebDevStudios/WDS-Simple-Page-Builder/blob/master/wds-simple-page-builder.php#L35-L38)
   [https://github.com/WebDevStudios/WDS-Simple-Page-Builder/blob/master/inc/functions.php#L29](https://github.com/WebDevStudios/WDS-Simple-Page-Builder/blob/master/inc/functions.php#L29)
   [https://github.com/WebDevStudios/WDS-Simple-Page-Builder/blob/master/inc/functions.php#L58-L90](https://github.com/WebDevStudios/WDS-Simple-Page-Builder/blob/master/inc/functions.php#L58-L90)
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [10 years, 10 months ago](https://wordpress.org/support/topic/using-cmb2-in-a-plugin/#post-6375371)
 * To add to what Justin mentioned, just at standard function/add_action inside 
   a basic plugin file is enough. The only difference between pasting it in the 
   example below and pasting it into functions.php is going to be the plugin comment
   block at the top, and the need to activate the plugin to have it work.
 * Edit: Don’t copy/paste this exactly, you’ll get undefined wds_page_builder_get_parts()
   function errors.
 *     ```
       <?php
       /*
       Plugin Name: Plugin Name
       Plugin URI: Plugin URI
       Description: Description of the plugin
       Version: 1.0
       Author: WebDevStudios
       Author URI: http://www.webdevstudios.com
       License: GPLv2
       Text Domain: Text domain to use
       */
   
       function davidshq( $meta_boxes ) {
   
       	$prefix = '_prefix_';
   
       	$cmb = new_cmb2_box( array(
       		'id'           => 'wds_simple_page_builder',
       		'title'        => esc_html__( 'Page Builder', 'wds-simple-page-builder' ),
       		'object_types' => array( 'post' ),
       		'context'      => 'normal',
       		'priority'     => 'high',
       		'show_names'   => true,
       	) );
       	$group_field_id = $cmb->add_field( array(
       		'id'           => $prefix . 'template',
       		'type'         => 'group',
       		'options'      => array(
       			'group_title'   => esc_html__( 'Template Part {#}', 'wds-simple-page-builder' ),
       			'add_button'    => esc_html__( 'Add another template part', 'wds-simple-page-builder' ),
       			'remove_button' => esc_html__( 'Remove template part', 'wds-simple-page-builder' ),
       			'sortable'      => true
       		)
       	) );
       	$cmb->add_group_field( $group_field_id, array(
       		'name'         => esc_html__( 'Template', 'wds-simple-page-builder' ),
       		'id'           => 'template_group',
       		'type'         => 'select',
       		'options'      => wds_page_builder_get_parts()
       	) );
   
       }
       add_action( 'cmb2_init', array( $this, 'do_meta_boxes' ) );
       ```
   
 *  Plugin Author [Justin Sternberg](https://wordpress.org/support/users/jtsternberg/)
 * (@jtsternberg)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/using-cmb2-in-a-plugin/#post-6375372)
 * As a follow up, you would NOT want to do the `if ( ! defined( 'CMB2_LOADED' )){}`
   check. just require cmb2/init.php and let CMB2 handle determining if it should
   load its version (or not).
 *  Plugin Contributor [Michael Beckwith](https://wordpress.org/support/users/tw2113/)
 * (@tw2113)
 * The BenchPresser
 * [10 years, 10 months ago](https://wordpress.org/support/topic/using-cmb2-in-a-plugin/#post-6375375)
 * And the other parts Justin just mentioned regarding loading CMB2 yourself instead
   of relying on it being the standalone plugin, if that’s the case.
 *  Thread Starter [Dave Mackey](https://wordpress.org/support/users/davidshq/)
 * (@davidshq)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/using-cmb2-in-a-plugin/#post-6375384)
 * Thanks Justin and Michael, this is very helpful! I’m up and running!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Using CMB2 in a Plugin?’ is closed to new replies.

 * ![](https://ps.w.org/cmb2/assets/icon.svg?rev=2866672)
 * [CMB2](https://wordpress.org/plugins/cmb2/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/cmb2/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/cmb2/)
 * [Active Topics](https://wordpress.org/support/plugin/cmb2/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/cmb2/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/cmb2/reviews/)

## Tags

 * [cmb2 plugin](https://wordpress.org/support/topic-tag/cmb2-plugin/)

 * 5 replies
 * 3 participants
 * Last reply from: [Dave Mackey](https://wordpress.org/support/users/davidshq/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/using-cmb2-in-a-plugin/#post-6375384)
 * Status: resolved