Roots Discourse

How to properly include CMB2 in sage 9

So I installed CMB2 with composer, but from looking at their github issues seems they aren’t actually supporting autoloading, so do we just require that sucker? or am I missing something?

Are you installing CMB2 with Composer within your Sage project directory? Or are you installing it as a plugin from the root of your WordPress (Bedrock?) project?

Since Sage uses composer-installers, it will install packages with "type": "wordpress-plugin" (i.e. CMB2) in ./wp-content/plugins relative to Sage’s root—which of course won’t work for us.

To install it as a package in Sage, add this to your composer.json to override where it’s installing CMB2 and to autoload it:

  //...
  "autoload": {
    //...
    "files": [
      "vendor/cmb2/cmb2/init.php"
    ]
  },
  //...
  "extra": {
    "installer-paths": {
      "vendor/cmb2/cmb2": ["cmb2/cmb2"]
    }
  },
  //...

Once you’ve made this change, run composer install to reinstall CMB2 in the right place and composer dumpautoload to make sure it works.

If you install it as a plugin at the WP/Bedrock level, you don’t need to worry about any of this.

3 Likes

Thanks, Knowler. I’m using Composer, this got it all sorted.

Hi. I’ve installed CMB2 in my project using the composer.json method you describe above but I can’t get examlpe-functions.php to load into my template. I’ve checked composer/autoload_files.php and CMB2 is showing up there so am presuming it’s loading when I start my project (yarn start)?

I’m trying to adding the following code:
require_once( dirname(__FILE__) . '/cmb2/examlpe-functions.php');
in my app/setup.php file but I get a 500 error when I refresh the browser… This is just so I can test that CMB2 is working, I would like to have a custom metabox file load from my assets folder once I know its working properly.

The above code worked when I used it in my function.php file using Sage 9.0.0-beta but this new version (Sage 9.0.1) is taking me some time to figure out…

Any help would be appreciated.

require_once( dirname(__FILE__) . '/cmb2/examlpe-functions.php');

You have misspelled “example” here, which may be an issue?

It’s unclear to me where the example-functions.php file comes from. Did you create it? Is it part of the CMB2 package? If the later, then your require snippet won’t work: Because you’ve put that snippet in app/setup.php, PHP is trying to require app/cmb2/examlpe-functions.php, which doesn’t exist. If that file is part of the CMB2 package, then it was installed in the vendor directory with the rest of the package.

Hello - I’m trying to add CMB2 to my Sage theme, but after updating the composer.json as suggested by Knowler, the vendor directory doesn’t include any “webdevstudios” directory in it…
What am I missing?

…just noticed that now is no more “webdevstudios/cmb2”, but “cmb2/cmb2”

1 Like

I updated to solution to reflect this. Thanks!

1 Like

Hello Knowler
…sorry to bother - I still try to test the CMB2 implementation using composer installation, but maybe I missed some points (I’m new to bedrock/sage, so for sure I need to read more about it). But I hope to get some help about the following:

  • I have installed the CMB2 using composer in Sage (not Bedrock);
  • then I’ve included the lines you mentioned above;
  • then, I’ve included some testing CMB2 metaboxes in app/setup.php of Sage theme, to test it;
  • now I receive the following message " Warning : call_user_func_array() expects parameter 1 to be a valid callback, function ‘cmb2_sample_metaboxes’ not found or invalid function name in …\site-name\web\wp\wp-includes\class-wp-hook.php on line 286";
  • this means that it can’t see the cmb2_admin_init, right?

For sure I’m totally missing the workflow here - so I hope that you guys could put me in the right direction…

Cheers from Italy…
Paolo

I checked their wiki and added:

"require": {
	"cmb2/cmb2": "dev-master",
},
"extra": {
	"installer-paths": {
		"vendor/cmb2/": ["cmb2/cmb2"]
	}
}

recommended when

including the library via composer in PSR-4 format (Example Antonella Framework)
in composer.json file

and that worked on latest Sage 9 like a charm.

NB You can include in setup.php with

require_once( __DIR__ . '/../vendor/cmb2/init.php' );
require_once( __DIR__ . '/../vendor/cmb2/example-functions.php');

But perhaps you want a mu-plugin instead…