1

I have followed the documentation on this page: https://github.com/WebDevStudios/CMB2/wiki/Basic-Usage but cannot get it to work.

I have gone through other posts with the same issue and tried all of the suggestions and still nothing. This has left me confused onthe installation process.

Can someone confirm whether the following code is needed?

if ( file_exists( DIR . '/cmb2/init.php' ) ) { require_once DIR . '/cmb2/init.php'; } elseif ( file_exists( DIR . '/CMB2/init.php' ) ) { require_once DIR . '/CMB2/init.php'; }

and also whether the cmb2 folder should be installed in the theme folder or the plugin folder. Documentation says theme, installation page on wordpress says plugin.

Also where should the code from example_functions.php go? Should it be in the functions.php file in the theme folder?

I have installed the plugin via wordpress and therefore the folder is installed in the plugins folder. I have then copied and pasted all of the code from example_functions.php into my functions.php file within my theme folder. I have then added the below code to the page.php file in my theme folder.

$text = get_post_meta( get_the_ID(), '_cmb2_text', true ); echo $text;

Please help.

  • Welcome to WPSE, @lea. Why don't you ask this question on Github, where the code is hosted? I think the author of the code could better help you – Lea Cohen Jan 20 '15 at 13:38
2

Step#1: Using git clone the latest CMB2 resources to your working project.
Step#2: Rename the CMB2-master (or anything else) folder to cmb.
Step#3: In your project's functions.php simply use the following code:

if( !class_exists("CMB2") ){
    require_once( dirname(__FILE__)."/libs/cmb/init.php" );
}

(I put all my external libraries to /themes/my-theme/libs/ so I called it so)

Step#4: You can include the example-functions.php from the cmb folder to check whether it's working or not. Simply use the following code in your functions.php:

require_once ( get_template_directory() . '/libs/cmb/example-functions.php' );

if it works then you will see example meta boxes in Add New Page, because by default it's coded like that: 'object_types' => array( 'page', ),.

Alternatively

Step#1: Install the CMB2 plugin from the WordPress plugin repository
Step#2: Browse the wp-content/plugins/cmb2/ (I can't remember the plugin folder name), and copy the example-functions.php to your theme for testing. Step#2: Simply use the following code in your functions.php to add the file:

require_once ( get_template_directory() . '/example-functions.php' );

If it works in Add New Page, you can do your own job. :)


For your custom usage just copy the function (cmb2_sample_metaboxes()) from the example and use the filter cmb2_meta_boxes. You can follow the example-functions.php completely. (In my installation it's in line#36 - see here)

  • I followed your alternative instructions and it works when i go to "Add new page" in the admin. However, before you mentioned that I was trying to get it to show on a page on the front end and added the following code to the page.php file but get nothing. $email = get_post_meta( get_the_ID(), '_cmb2_email', true ); echo $email; – lea Jan 20 '15 at 14:36
  • If you din't entered any data within any page, then you won't get any result fetching them. First enter some data using custom fields from back end. Anyway, if it helps you can accept the answer by clicking the tick mark on the left. :) – Mayeenul Islam Jan 20 '15 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.